I am building a Java swing program and I installed jfreechart dependency to show charts. Most part of the chart is done, but i can't figure out how to change the colour of the line for negative and positive values. (e.g positive values Green colour and for negative Red colour) (I don't want to change the background) How to do it?
And while adding values i would want to hide the higher shown numbers in the X axis so it should only show the last added data's value (in the below exmaple image this is 48, it shoould hide the 50 on the axis) (It is programatically adding values to the chart, it uses some ranging on the axis this is why it shows a higher val that the latest added val)
Exmaple code:
//imports
import java.util.*;
import java.awt.Color;
import java.awt.Font;
import java.awt.Shape;
import java.awt.geom.Rectangle2D;
import java.text.DecimalFormat;
import javax.swing.JPanel;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.ValueMarker;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYSplineRenderer;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
//public class main extends javax.swing.JFrame {
int count = 0;
double amount = 0.00000000;
XYSeries series = new XYSeries("dataset");
XYSeries series2 = new XYSeries("");
public JPanel createDemoPanel() {
// create plot...
NumberAxis xAxis = new NumberAxis("");
xAxis.setAutoRangeIncludesZero(true);
xAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
NumberAxis yAxis = new NumberAxis("");
yAxis.setAutoRangeIncludesZero(true);
DecimalFormat format = new DecimalFormat("#.########");
yAxis.setNumberFormatOverride(format);
XYSplineRenderer renderer1 = new XYSplineRenderer();
renderer1.setSeriesShapesVisible(0, false);
double size = 20.0;
double delta = size / 2.0;
Shape shape1 = new Rectangle2D.Double(-delta, -delta, size, size);
renderer1.setSeriesShape(0, shape1);
XYPlot plot = new XYPlot(createSampleData(), xAxis, yAxis, renderer1);
plot.setBackgroundPaint(Color.white);
plot.setDomainGridlinesVisible(false);
plot.setRangeGridlinesVisible(false);
ValueMarker marker1 = new ValueMarker(0);
marker1.setPaint(Color.black);
plot.addRangeMarker(marker1);
// create and return the chart panel...
JFreeChart chart = new JFreeChart(null,
JFreeChart.DEFAULT_TITLE_FONT, plot, false);
//ChartUtilities.applyCurrentTheme(chart);
ChartPanel chartPanel = new ChartPanel(chart);
return chartPanel;
}
private XYDataset createSampleData() {;
series.add(0, 0.00000000);
XYSeriesCollection result = new XYSeriesCollection(series);
series2.add(0, 0);
result.addSeries(series2);
return result;
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
count = count + 1;
Random rand = new Random();
if(rand.nextBoolean() == false){
amount -= 0.00001;
}
else
{
amount += 0.00001;
}
series.add(count, amount);
this.jLabel1.setText("Status: " + count);
}
