MindFusion Tutorial: FunctionSeries in Charting for Java Swing

MindFusion.Diagramming for WinForms is a .NET control that can help you create workflow and process diagrams; database entity relationship diagrams; organization diagrams; object hierarchy and relationship diagrams; diagrams and trees. It is based on the types of objects-chart boxes, tables and arrows, which are classified and assigned to others and combined into a complex structure. This control provides more than 50 pre-defined chart boxes, such as custom design styles and coloring chart boxes.

Click to download the latest trial version of MindFusion.Diagramming for WinForms

In this blog post, we will build the chart you see in the image below:
Insert picture description here

The next section will provide you with a step-by-step guide on how to build this quadratic equation chart from scratch.

I. Project settings

Create a new Java project in your preferred Java IDe and copy the JChart.jar file in the project folder. This is the *.jar archive that contains the library. In our example, we have copied it into a new subfolder called "Library". JChart.jar is found in the downloadable archive of the trial version of MindFusion Charting for Java Swing.

We need to include JChart.jar in the build path of the charting application. In Eclipse, we do this by right-clicking on the project root folder and selecting "Properties -> Java Build Path -> Add JAR", and then navigate to the location of JChart.lib in the project folder:

Insert picture description here

Then, we create a new class, call it MainWindow and make it extend JFrame. This will be the only class of our application, and it will start it:

 public class MainWindow extends JFrame class MainWindow extends JFrame
{
    
    {
    
    
   private static final long serialVersionUID = 1L;private static final long serialVersionUID = 1L;

  //the main method of the application//the main method of the application
   public static void main(String[] args)public static void main(String[] args)
   {
    
    {
    
    
      SwingUtilities.invokeLater(new Runnable()SwingUtilities.invokeLater(new Runnable()
{
    
    {
    
    
     public void run()public void run()
     {
    
    {
    
    
trytry
{
    
    {
    
    
   new MainWindow().setVisible(true);new MainWindow().setVisible(true);
}}
catch (Exception exp)catch (Exception exp)
{
    
    {
    
    
}}
  }}
}); }); 
    }}
}}

We import the namespaces that will be used: they are used both for charts and standard AWT and Swing namespaces:
i

mport java.awt.BorderLayout; java.awt.BorderLayout;
import java.awt.Color;import java.awt.Color;
import java.util.Arrays;import java.util.Arrays;

import javax.swing.JFrame;import javax.swing.JFrame;
import javax.swing.SwingUtilities;import javax.swing.SwingUtilities;

import com.mindfusion.charting.FunctionSeries;import com.mindfusion.charting.FunctionSeries;
import com.mindfusion.charting.GridType;import com.mindfusion.charting.GridType;
import com.mindfusion.charting.swing.LineChart;import com.mindfusion.charting.swing.LineChart;
import com.mindfusion.drawing.Brushes;import com.mindfusion.drawing.Brushes;
import com.mindfusion.drawing.DashStyle;import com.mindfusion.drawing.DashStyle;
import com.mindfusion.drawing.SolidBrush;import com.mindfusion.drawing.SolidBrush;

After finishing, we can create the chart.

2. Line chart

We create LineChart is a separate method, we will call this method from the constructor of the main class.

private LineChart initializeChart() LineChart initializeChart()
{
    
    {
    
    
LineChart lineChart = new LineChart(); LineChart lineChart = new LineChart(); 
        ....................................
        ....................................
        return lineChart;return lineChart;
}}

The mathematical function graph we want to present will be drawn by the FunctionSeries class. Its constructor takes the mathematical expression defining the function equation as a string. Then, it parses it through the MindFusion.Scripting library included in JChart. In our example, we created two FunctionSeries, which use two different quadratic equations:

FunctionSeries series1; series1;
FunctionSeries series2; FunctionSeries series2; 

trytry
{
    
     {
    
     

series1 = new FunctionSeries(= new FunctionSeries(
"3*x*x+2*x+2", 1000, -5, 5 );"3*x*x+2*x+2", 1000, -5, 5 );
series1.setTitle("Quadratic equation: 3*x*x+2*x+2");.setTitle("Quadratic equation: 3*x*x+2*x+2");
lineChart.getSeries().add(series1);.getSeries().add(series1);

//mean value is -2//mean value is -2
series2 = new FunctionSeries(= new FunctionSeries(
"-3*x*x+4*x+1", 1000, -5, 5);"-3*x*x+4*x+1", 1000, -5, 5);
series2.setTitle("Quadratic equation: -3*x*x+4*x+1");.setTitle("Quadratic equation: -3*x*x+4*x+1");
lineChart.getSeries().add(series2);.getSeries().add(series2);

}}
catch (Exception e)catch (Exception e)
{
    
    {
    
    
// could not parse the expression// could not parse the expression
e.printStackTrace();.printStackTrace();
}}

Note that the constructor of FunctionSeries is contained in a try-catch block. If you cannot resolve the equation, you can do so. In this case, an exception will be printed to provide information about the error.
The other parameters provided to the FunctionSeries constructor are: 1. The number of points to be calculated for the function graph; 2. The start of the calculation interval; 3. The end of the calculation interval. The calculation includes start and end values.

Next, we get the ContentPane for JFrame and use BorderLayout to extend the LineChart to its entire work area:

protected MainWindow() MainWindow()
{
    
    {
    
    
setDefaultCloseOperation(EXIT_ON_CLOSE);(EXIT_ON_CLOSE);
setSize(650, 400);(650, 400);
setTitle("Java Swing Library for Charts and Gauges: FunctionSeries"); ("Java Swing Library for Charts and Gauges: FunctionSeries"); 
//add the chart to the ContentPane//add the chart to the ContentPane
getContentPane().add(initializeChart(), BorderLayout.CENTER);().add(initializeChart(), BorderLayout.CENTER);
}}

Now, if we run the example, we can see the graph of the chart even without a style:
Insert picture description here

We will use the many style functions of the Java chart library to make the chart more visually attractive.

Three. Style Sheet

We first use the getXAxis and getYAxis methods to access the two main axes of the chart and set its style.

lineChart.getXAxis().setMinValue(-5.0);.getXAxis().setMinValue(-5.0);
lineChart.getXAxis().setMaxValue(5.0);.getXAxis().setMaxValue(5.0);
lineChart.getXAxis().setInterval(0.5);.getXAxis().setInterval(0.5);
lineChart.getXAxis().setOrigin(0.0);.getXAxis().setOrigin(0.0);
lineChart.getXAxis().setTitle("X-axis");.getXAxis().setTitle("X-axis");

lineChart.getYAxis().setMaxValue(30.0);.getYAxis().setMaxValue(30.0);
lineChart.getYAxis().setMinValue(-30.0);.getYAxis().setMinValue(-30.0);
lineChart.getYAxis().setTitle("Y-axis");.getYAxis().setTitle("Y-axis");

We use the setMinValue and setMaxValue methods to specify the two ends of the axis. If we do not explicitly specify the interval using setInterval, the chart will calculate it so that there are exactly 10 intervals between the minimum and maximum. We also use setTitle to specify how to call the axis.
Then, we specify the GridType of the chart and the strokes used when rendering the grid. We want to cross the grid:

//styling the grid
lineChart.setGridType(GridType.Crossed);.setGridType(GridType.Crossed);
lineChart.getTheme().setGridLineColor(new Color(192, 192, 192));.getTheme().setGridLineColor(new Color(192, 192, 192));
lineChart.getTheme().setGridLineStyle(DashStyle.Dot);.getTheme().setGridLineStyle(DashStyle.Dot);

Defaut draws the legend using the legend label obtained from the Title of the Series instance of the chart. We want to hide the title of the legend and increase the font of the legend label. The properties are LegendRenderer and DataLabelsFontSize of ShowTitle.

lineChart.getLegendRenderer().setShowTitle(false);.getLegendRenderer().setShowTitle(false);
lineChart.getTheme().setDataLabelsFontSize(12);.getTheme().setDataLabelsFontSize(12);

Finally, we need to set the color of the chart series. There are many styles to choose from, but in our case, we need to simply color each series with a single-color brushstroke. We use CommonSeriesStrokes and CommonSeriesStrokeThicknesses to specify the color and thickness of the graph line. We also set CommonSeriesFills to use the same brush set as the stroke. We don't need to use them through chart rendering, but the legend uses them to draw color squares before the series title:

lineChart.getTheme().setHighlightStroke(Brushes.Orange);.getTheme().setHighlightStroke(Brushes.Orange);
lineChart.getTheme().setCommonSeriesStrokes(.getTheme().setCommonSeriesStrokes(
Arrays.asList(Arrays.asList(
new SolidBrush( new Color (90, 116, 68 )),new SolidBrush( new Color (90, 116, 68 )),
new SolidBrush( new Color (70, 105, 125))));new SolidBrush( new Color (70, 105, 125))));
lineChart.getTheme().setCommonSeriesFills(.getTheme().setCommonSeriesFills(
Arrays.asList(Arrays.asList(
new SolidBrush( new Color (90, 116, 68 )),new SolidBrush( new Color (90, 116, 68 )),
new SolidBrush( new Color (70, 105, 125))));new SolidBrush( new Color (70, 105, 125))));
lineChart.getTheme().setCommonSeriesStrokeThicknesses(.getTheme().setCommonSeriesStrokeThicknesses(
Arrays.asList(3.0));Arrays.asList(3.0));

These are the last few lines of code in the charting application. Now, we have a beautiful quadratic function graph. This is the complete project source code and the trial version of JChart.jar:
For technical support, please use the discussion area of ​​the Java Swing chart library at https://mindfusion.eu/Forum/YaBB.pl?board=jchart_dic

About MindFusion chart and specification library for Java Swing: This is a native Java Swing library that is suitable for drawing a variety of charts and specifications. The flexible API allows to combine various chart parts: axes, graphs and series to create unique charts that fit the specific needs of any business application: charts with multiple graphs, axes in all directions, different chart graphics in one chart, etc. . All chart series come from the basic "series" interface, which programmers can implement to create their own series classes. The appearance is controlled by the theme, and the appearance of the chart can be customized. The gauge library is part of the chart control and provides a set of elliptical and linear gauges, which can be used to create any type of gauge (round or rectangular gauge), with up to three gauges. The library comes with a set of predefined popular instruments: compass, clock, thermometer, etc.

Click >> View original text

Guess you like

Origin blog.csdn.net/RoffeyYang/article/details/114081234