Ok so I have a project for work which aims to provide an application to display response time/call volumes for online server. I have all of the backend complete where I retrieve/format stats to feed into graphs.
I am stuck on **changing the color of the lines!*, which is strange because line colours are automatically different in this exmaple: http://www.java2s.com/Code/Java/Chart/J ... rtDemo.htm. Find the source code for my graph creation class below. Any help would be much, much appreciated (I am working on this on a Sunday as I did not want it to run into the New Year!!)
Code: Select all
package osv.onlinestatviewer;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.List;
import java.util.ArrayList;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.CategoryLabelPositions;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.DatasetRenderingOrder;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.AreaRenderer;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.chart.renderer.category.CategoryItemRenderer;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;
/**
* A simple demonstration application showing how to create a bar chart overlaid
* with a line chart.
*/
//public class AreaLineGraph extends ApplicationFrame {
public class AreaLineGraph {
ChartPanel chartPanel;
CategoryPlot plot;
CategoryPlot plot2;
int chartIndex = 0;
String title;
static Color colors[] = {Color.BLACK, Color.BLUE, Color.CYAN, Color.GREEN,
Color.GRAY, Color.MAGENTA, Color.ORANGE, Color.PINK, Color.RED,
Color.WHITE, Color.YELLOW, Color.LIGHT_GRAY };
public AreaLineGraph(String t) {
title = t;
plot = new CategoryPlot();
plot.setDomainAxis(0, new CategoryAxis("Hours"));
plot.setRangeAxis(0, new NumberAxis("Volumes"));
plot.setRangeAxis(1, new NumberAxis("Response Times"));
plot.setOrientation(PlotOrientation.VERTICAL);
plot.setRangeGridlinesVisible(true);
plot.setDomainGridlinesVisible(true);
}
public void addDataSet(DefaultCategoryDataset ds, String type, CategoryItemRenderer r, int axisIndex)
{
//r.setSeriesPaint(chartIndex, colors[chartIndex]);
plot.setDataset(chartIndex, ds);
plot.setRenderer(chartIndex, r);
plot.mapDatasetToRangeAxis(chartIndex, axisIndex);
chartIndex++;
}
public void finalise()
{
plot.setDatasetRenderingOrder(DatasetRenderingOrder.REVERSE);
plot.getDomainAxis().setCategoryLabelPositions(CategoryLabelPositions.UP_45);
plot.getDomainAxis().setCategoryMargin(0);
for (int i = 0; i < plot.getDatasetCount(); i++) {
plot.getRendererForDataset(plot.getDataset(i)).setSeriesPaint(0, colors[i]);
}
final JFreeChart chart = new JFreeChart(plot);
chart.setTitle(title);
chartPanel = new ChartPanel(chart);
chartPanel.setPreferredSize(new java.awt.Dimension(930, 500));
}
public int getChartIndex()
{
return chartIndex;
}
public ChartPanel getChartPanel()
{
return chartPanel;
}
}
Added snippets from my main class as requested. I have not included it all as there is a fair bit of unfinished code related to the rest of the application.
Code: Select all
public static void createDailyGraph() throws FileNotFoundException {
String type = "";
String next = "";
int index = 0;
boolean start = false;
DefaultCategoryDataset ds = new DefaultCategoryDataset();
CategoryItemRenderer areaR = new AreaRenderer();
CategoryItemRenderer lineR = new LineAndShapeRenderer();
alg = new AreaLineGraph("Daily Graph Test");
file = new File("C:\\Program Files\\OnlineStatViewer\\stats\\server_totals\\API059\\API059_SERVER_TOTALS_2013-12-12");
scanner = new Scanner(file);
// Creating datasets and adding them to the graph
while (scanner.hasNext()) {
next = scanner.next();
if (!isNum(next)) {
if(start) {
alg.addDataSet(ds, lineR, 1);
//System.out.println(type);
ds = new DefaultCategoryDataset();
}
else { start=true; }
type=next;
index=0;
} else {
ds.addValue(Double.parseDouble(next), type, hours[index]);
index++;
}
}
alg.addDataSet(ds, areaR, 0); // after adding all of the other datasets I add the final one which is the total number of transactions
alg.finalise();
cp = alg.getChartPanel();
displayPanel.add(cp, "CARD");
}
public static boolean isNum(String str)
{
if(Character.isDigit(str.charAt(0))) {
return true;
} return false;
}
Code: Select all
servername 0.060 0.050 0.036 0.180 0.175 0.180 0.216 0.252 0.192 0.184 0.174 0.210 0.240 0.140 0.137 0.077 0.035