3.2 Simple Chart Source Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import java.awt.*;
import javax.swing.JFrame;
import lt.monarch.chart.chart2D.*;
import lt.monarch.chart.chart2D.axis.*;
import lt.monarch.chart.chart2D.engine.PlaneMapper2D;
import lt.monarch.chart.chart2D.series.BarSeries;
import lt.monarch.chart.engine.ChartObject;
import lt.monarch.chart.legend.Legend;
import lt.monarch.chart.mapper.LabelAxisMapper;
import lt.monarch.chart.mapper.MathAxisMapper;
import lt.monarch.chart.models.ChartDataModel;
import lt.monarch.chart.style.enums.*;
import lt.monarch.chart.style.tags.AxisTextPaintTags;
import lt.monarch.chart.swing.JChartPanel;
import lt.monarch.chart.view.LabeledChart;
 
public class QuickStartDemonstration extends JFrame
{
 
	public QuickStartDemonstration()
	{
		// Setting chart's data
		// Animals will represent KEYS on X-axis
		String[] animals = { "Cheetah", "Zebra", "Human", "Elephant",
                                     "Chicken" };
		// Speeds will represent VALUES on Y-axis
		Integer[] speeds = { new Integer(70), new Integer(40), new Integer(28)
                , new Integer(25), new Integer(9) };
		// Chart data Model creation
		ChartDataModel dataModel = new ChartDataModel(new Object[][] { animals
		, speeds });
		// Maps integers from 0 to 80 on to the y axis
		MathAxisMapper yMapper = new MathAxisMapper(0, 80);
 
		// Maps labels on to the x axis
		LabelAxisMapper xMapper = new LabelAxisMapper(animals);
 
		// Setting Bars
		BarSeries bars = new BarSeries(dataModel, xMapper, yMapper);
		// Setting bars' background and foreground colors
		bars.style.setBackground(new Color(30,90,255));
		bars.style.setForeground(new Color(30,90,255));
		// Setting BarSeries title which will show up in legend
		bars.setName("Speed");
 
		// Setting X-axis
		Axis2DX axisX = new Axis2DX(xMapper);
		// Setting X-axis title which will be shown on chart
		axisX.setTitle("Animal types");
		// Setting X-axis title position
		axisX.getTitleSettings().setPosition(TitlePosition.BELOW);
		// Setting X-axis fonts
		axisX.getTextStyle().setFont(AxisTextPaintTags.LABEL,
		new Font("dialog", Font.PLAIN, 10));
		axisX.style.setFont("title", new Font("dialog", Font.PLAIN, 15));
		// Setting Y-axis
		Axis2DY axisY = new Axis2DY(yMapper);
		// Setting Y-axis title which will be shown on chart
		axisY.setTitle("Speed, Mph");
		// Setting Y-axis title position
		axisY.getTitleSettings().setPosition(TitlePosition.BELOW);
		// Setting Y-axis fonts
		axisY.getTextStyle().setFont(AxisTextPaintTags.LABEL, 
		new Font("dialog", Font.PLAIN, 10));
		axisY.style.setFont("title", new Font("dialog", Font.PLAIN, 15));
		// Horizontal grid creation
		// We do not specify the x axis because we want only horizontal
                //lines to be painted.
		Grid grid = new Grid(new PlaneMapper2D(), null, yMapper);
		// Chart object creation and configuration
		Chart2D chart = new Chart2D();
		// Setting objects which will be drawn
		chart.setObjects(new ChartObject[] { grid, bars});
		// Setting chart's axis
		chart.setXAxis(axisX);
		chart.setYAxis(axisY);
 
		// Setting legend
		Legend legend = new Legend(chart);
		// Setting legend's fonts and colors
		legend.style.setFont("legend", new Font("dialog", Font.PLAIN, 8));
		legend.style.setBackground(Color.WHITE);
 
		// Setting Main chart
		LabeledChart topChart = new LabeledChart(chart);
		// Setting title
		topChart.setTitle("Animal Speeds");
		// Setting legend's position on chart
		topChart.setBottomView(legend);
 
		getContentPane().add(BorderLayout.CENTER, new JChartPanel(topChart));
	}
 
	public static void main(String[] args)
	{
		QuickStartDemonstration frame = new QuickStartDemonstration();
		frame.setSize(new Dimension(600, 400));
		frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
		frame.setTitle("MCharts QuickStart Demonstration");
		frame.setVisible(true);
	}
}