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 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 | 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.LineSeries;
import lt.monarch.chart.engine.ChartObject;
import lt.monarch.chart.legend.Legend;
import lt.monarch.chart.mapper.*;
import lt.monarch.chart.marker.*;
import lt.monarch.chart.models.*;
import lt.monarch.chart.swing.JChartPanel;
import lt.monarch.chart.view.LabeledChart;
public class LabeledMarkerDemo extends JFrame {
private transient JChartPanel m_chartPanel;
private transient Chart2D chart;
public LabeledMarkerDemo() {
setLayout(new BorderLayout());
initChart();
add(m_chartPanel, BorderLayout.CENTER);
}
protected void initChart() {
/*
* Setting data models
*/
String labels[] = { "2006Q01", "2006Q02", "2006Q03", "2006Q04", "2007Q01", "2007Q02", "2007Q03", "2007Q04", "2008Q01", "2008Q02", "2008Q03", "2008Q04" };
Double[] valLV = { 1704.6, 1683.0, 1654.3, 1707.5, 1790.6, 1753.0, 1729.9, 1911.7, 2325.4, 2432.2, 2614.3, 4468.1 };
Double[] valLT = { 3911.4, 3867.0, 3843.5, 4326.5, 4382.1, 4350.3, 4249.4, 4836.1, 4442.4, 4649.5, 4595.1, 5032.1 };
ChartDataModel modelLV = new ChartDataModel();
for (int i = 0; i < labels.length; i++) {
modelLV.add(new Object[] { labels[i], valLV[i] });
}
ChartDataModel modelLT = new ChartDataModel();
for (int i = 0; i < labels.length; i++) {
modelLT.add(new Object[] { labels[i], valLT[i] });
}
/*
* Creating mappers
*/
LabelAxisMapper xMapper = new LabelAxisMapper(labels);
MathAxisMapper yMapper = new MathAxisMapper(1000, 6000);
/*
* Creating and setting the axis
*/
Axis2DX axisX = new Axis2DX(xMapper);
axisX.getLabelSettings().setRotationAngle(Math.toRadians(-90));
Axis2DY axisY = new Axis2DY(yMapper);
axisY.setTitle("Millions of euro");
MetaDataModel metaLV = new MetaDataModel();
for (int i = 0; i < labels.length; i++) {
metaLV.setData(MetaDataType.LABEL, i, valLV[i]);
metaLV.setData(MetaDataType.NUMBER_FORMAT, i, "##");
}
MetaDataModel metaLT = new MetaDataModel();
for (int i = 0; i < labels.length; i++) {
metaLT.setData(MetaDataType.LABEL, i, valLT[i]);
metaLT.setData(MetaDataType.NUMBER_FORMAT, i, "##");
}
/*
* Creating Markers
*/
DotMarker markerLV = new DotMarker();
markerLV.getPaintStyle().setBackground(new Color(49, 119, 31));
markerLV.getPaintStyle().setForeground(new Color(49, 119, 31, 180));
LabeledMarker mLV = new LabeledMarker(markerLV, metaLV);
mLV.getTextStyle().setColor(Color.black);
mLV.getPaintStyle().setBackground(null);
mLV.getPaintStyle().setForeground(null);
DotMarker markerLT = new DotMarker();
markerLT.getPaintStyle().setBackground(new Color(143, 60, 168));
markerLT.getPaintStyle().setForeground(new Color(143, 60, 168, 180));
LabeledMarker mLT = new LabeledMarker(markerLT, metaLT);
mLT.getTextStyle().setColor(Color.black);
mLT.getPaintStyle().setBackground(null);
mLT.getPaintStyle().setForeground(null);
/*
* Setting Series
*/
LineSeries lineLV = new LineSeries(modelLV, new PlaneMapper2D(), xMapper, yMapper);
lineLV.setName("Latvia");
lineLV.style.setForeground(new Color(49, 119, 31));
lineLV.style.setBackground(new Color(49, 119, 31, 60));
lineLV.style.setStroke(new BasicStroke(3));
lineLV.addMarker(mLV);
LineSeries lineLT = new LineSeries(modelLT, new PlaneMapper2D(), xMapper, yMapper);
lineLT.setName("Lithuania");
lineLT.style.setForeground(new Color(143, 60, 168));
lineLT.style.setBackground(new Color(143, 60, 168, 40));
lineLT.style.setStroke(new BasicStroke(3));
lineLT.addMarker(mLT);
/*
* Creating grid
*/
Grid grid = new Grid(xMapper, yMapper);
/*
* Creating the chart and add objects
*/
chart = new Chart2D();
chart.setObjects(new ChartObject[] { grid, axisX, axisY, lineLV, lineLT });
chart.setYAxis(axisY);
chart.setXAxis(axisX);
/*
* Set the title of chart
*/
LabeledChart topChart = new LabeledChart(chart);
topChart.setTitle("General Government Consolidated Gross Debt");
/*
* Creating the legend
*/
Legend legend = new Legend(chart);
legend.style.setBackground(Color.white);
legend.setMaxColumns(1);
legend.getPaintStyle().setBackground(null);
legend.getPaintStyle().setForeground(null);
topChart.setRightView(legend);
/*
* Adding topChart to panel
*/
m_chartPanel = new JChartPanel(topChart);
}
public static void main(String[] args) {
LabeledMarkerDemo frame = new LabeledMarkerDemo();
frame.setSize(new Dimension(600, 400));
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.setTitle("General Government Consolidated Gross Debt");
frame.setVisible(true);
}
} |