public JPanel createDemo() { model = new DefaultChartModel(); demoPanel = new JPanel(); Axis xAxis = new CategoryAxis<Country>(countries); xAxis.setLabel("Countries"); Axis yAxis = new Axis(new NumericRange(0, 25)); yAxis.setLabel("Numbers"); chart = new Chart(); chart.setXAxis(xAxis); chart.setYAxis(yAxis); Font titleFont = UIManager.getFont("Label.font").deriveFont(Font.BOLD, 14f); chart.setTitle( new AutoPositionedLabel("Chart with Categorical X Values", Color.blue, titleFont)); chart.setVerticalGridLinesVisible(false); chart.setShadowVisible(true); model .addPoint(usa, 22) .addPoint(uk, 18) .addPoint(france, 13.5) .addPoint(germany, 12) .addPoint(russia, 8) .addPoint(china, 7); Color green = new Color(0, 170, 0); ChartStyle style = new ChartStyle(green, true, true); style.setLineWidth(6); style.setPointSize(20); chart.addModel(model, style).setPointRenderer(new SphericalPointRenderer()); ChartStyle redHighlightStyle = new ChartStyle(new Color(200, 0, 0), PointShape.DISC, 20).withPointsAndLines(); redHighlightStyle.setLineWidth(6); redHighlightStyle.setLineColor(green); chart.setHighlightStyle(redHighlight, redHighlightStyle); demoPanel.setLayout(new BorderLayout()); demoPanel.add(chart, BorderLayout.CENTER); chart.addMouseMotionListener( new MouseMotionListener() { // Allow the user to drag _points in the vertical direction public void mouseDragged(MouseEvent e) { rollover(e); if (highlighted != null) { Point p = e.getPoint(); Point2D userPoint = chart.calculateUserPoint(p); if (userPoint != null) { highlighted.setY(new RealPosition(userPoint.getY())); } chart.repaint(); } } // Add a rollover effect public void mouseMoved(MouseEvent e) { rollover(e); } private void rollover(MouseEvent e) { Point p = e.getPoint(); PointSelection selection = chart.nearestPoint(p, model); if (highlighted != null) { highlighted.setHighlight(null); } Chartable selected = selection.getSelected(); Point2D selectedCoords = new Point2D.Double(selected.getX().position(), selected.getY().position()); Point dp = chart.calculatePixelPoint(selectedCoords); // Only activate the rollover effect when within 50 pixels of a point if (p.distance(dp) < 50) { highlighted = (ChartPoint) selection.getSelected(); highlighted.setHighlight(redHighlight); ChartCategory<?> x = (ChartCategory<?>) selected.getX(); chart.setToolTipText( String.format("%s : %.1f", x.getName(), selected.getY().position())); } else { chart.setToolTipText(null); } chart.repaint(); } }); chart.addMouseListener( new MouseAdapter() { @Override public void mouseExited(MouseEvent e) { if (highlighted != null) { highlighted.setHighlight(null); chart.repaint(); } } }); demoPanel.setPreferredSize(new Dimension(500, 500)); return demoPanel; }