This marker allow displaying images on data points. In order to achieve this, either a local image path or URL should be set.
1 | //setting ImageMarker object ImageMarker marker = new ImageMarker(); //setting image marker.setImage("lt.jpg"); //setting image size in pixels marker.setMarkerSize(20,15); |
Figure 5.6.4.1 Image Markers Demonstration
1 | import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.net.MalformedURLException; import java.net.URL; import javax.swing.JFrame; import lt.monarch.chart.chart2D.Chart2D; import lt.monarch.chart.chart2D.Grid; import lt.monarch.chart.chart2D.axis.Axis2DX; import lt.monarch.chart.chart2D.axis.Axis2DY; 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.LabelAxisMapper; import lt.monarch.chart.mapper.MathAxisMapper; import lt.monarch.chart.marker.ImageMarker; import lt.monarch.chart.models.ChartDataModel; import lt.monarch.chart.swing.JChartPanel; import lt.monarch.chart.view.LabeledChart; public class ImageMarkerDemo extends JFrame { private transient JChartPanel m_chartPanel; public ImageMarkerDemo() { setLayout(new BorderLayout()); initChart(); add(m_chartPanel, BorderLayout.CENTER); } void initChart() { /* * Creating mappers */ String labels[] = { "2003", "2004", "2005", "2006", "2007", "2008" }; LabelAxisMapper xMapper = new LabelAxisMapper(labels); MathAxisMapper yMapper = new MathAxisMapper(0, 15000); /* * Creating and setting the axis */ Axis2DX axisX = new Axis2DX(xMapper); axisX.setTitle("Year"); Axis2DY axisY = new Axis2DY(yMapper); axisY.setTitle("Millions of euro"); /* * Setting Series */ LineSeries lineLT = new LineSeries(generateLTModel(), new PlaneMapper2D(), xMapper, yMapper); lineLT.setName("Lithuania"); lineLT.style.setForeground(new Color(0, 106, 68)); /* * Creating Image Marker */ ImageMarker img = new ImageMarker(); img.style.setForeground(null); img.setMarkerSize(20, 15); try { img .setImage("lt.jpg"); } catch (MalformedURLException e1) { e1.printStackTrace(); } lineLT.addMarker(img); LineSeries lineLV = new LineSeries(generateLVModel(), new PlaneMapper2D(), xMapper, yMapper); lineLV.setName("Latvia"); lineLV.style.setForeground(new Color(128, 0, 38)); /* * Creating Image Marker */ ImageMarker img2 = new ImageMarker(); img2.style.setForeground(null); img2.setMarkerSize(20, 15); try { img2 .setImage("lv.jpg"); } catch (MalformedURLException e) { e.printStackTrace(); } lineLV.addMarker(img2); /* * Creating grid */ Grid grid = new Grid(xMapper, yMapper); /* * Creating the chart and add objects */ Chart2D chart = new Chart2D(); chart.setObjects(new ChartObject[] { grid, axisX, axisY, lineLT, lineLV, }); chart.setYAxis(axisY); chart.setXAxis(axisX); /* * Set the title of chart */ LabeledChart topChart = new LabeledChart(chart); topChart.setTitle("Total General Government Expenditure"); /* * Creating the legend */ Legend legend = new Legend(chart); legend.style.setBackground(Color.white); legend.setMaxColumns(3); topChart.setBottomView(legend, 10); /* * Adding topChart to panel */ m_chartPanel = new JChartPanel(topChart); } private ChartDataModel generateLTModel() { ChartDataModel modelZ = new ChartDataModel(); modelZ.add(new Object[] { "2003", 5472.0 }); modelZ.add(new Object[] { "2004", 6051.6 }); modelZ.add(new Object[] { "2005", 6957.9 }); modelZ.add(new Object[] { "2006", 8052.6 }); modelZ.add(new Object[] { "2007", 9932.8 }); modelZ.add(new Object[] { "2008", 12026.3 }); return modelZ; } private ChartDataModel generateLVModel() { ChartDataModel modelZ = new ChartDataModel(); modelZ.add(new Object[] { "2003", 3470.6 }); modelZ.add(new Object[] { "2004", 3998.2 }); modelZ.add(new Object[] { "2005", 4629.8 }); modelZ.add(new Object[] { "2006", 6126.7 }); modelZ.add(new Object[] { "2007", 7585.5 }); modelZ.add(new Object[] { "2008", 9119.8 }); return modelZ; } public static void main(String[] args) { ImageMarkerDemo frame = new ImageMarkerDemo(); frame.setSize(new Dimension(600, 400)); frame.setDefaultCloseOperation(EXIT_ON_CLOSE); frame.setTitle("Total General Government Expenditure"); frame.setVisible(true); } } |