2015-12-30 15 views
2

我有一个CSV文件,它看起来像这样(the file can be be downloaded here)

enter image description here
根据值更改图表点色 - JFreeChart的

我想在X轴上与时间曲线图本(电子表格中的第一列)以及Y轴上开关的名称(除第一列以外的所有列标题)。我希望为每列在Y轴上创建一个点。列将是平行于X轴,但颜色应根据值TRUE或FALSE,或0,1改变,2

这是我到目前为止已经开发出:

package democsv; 

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.util.ArrayList; 
import java.util.Arrays; 
import java.util.List; 
import org.jfree.chart.ChartFactory; 
import org.jfree.chart.ChartPanel; 
import org.jfree.chart.JFreeChart; 
import org.jfree.chart.plot.PlotOrientation; 
import org.jfree.chart.plot.XYPlot; 
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer; 
import org.jfree.data.xy.XYDataset; 
import org.jfree.data.xy.XYSeries; 
import org.jfree.data.xy.XYSeriesCollection; 
import org.jfree.ui.ApplicationFrame; 
import org.jfree.ui.RefineryUtilities; 

public class DemoCSV extends ApplicationFrame { 

    public static List<Object> sections = new ArrayList<>(); 
    public static String[] switchNames = null; 
    public static ArrayList<String> time = new ArrayList<>(); 
    public static ArrayList<ArrayList<String>> datastore = new ArrayList<>(); 

    public DemoCSV(String applicationTitle, String chartTitle) throws IOException { 
     super(applicationTitle); 
     InputStream in = DemoCSV.class.getResourceAsStream("/files/Electrical.csv"); 
     InputStream in2 = DemoCSV.class.getResourceAsStream("/files/Electrical.csv"); 

     BufferedReader reader = new BufferedReader(new InputStreamReader(in)); 
     BufferedReader read = new BufferedReader(new InputStreamReader(in2)); 
     String line, line2; 
     int cnt = 0; 

     while ((line = reader.readLine()) != null) { 
      String[] dataIn = line.split(","); 

      if (cnt == 0) { 
       switchNames = sNames(dataIn); 
      } else if (cnt != 0) { 
       time.add(dataIn[0]); 
      } 
      cnt++; 
     } 

     cnt = 0; 

     int dsize = switchNames.length; 

     while (datastore.size() < dsize) { 
      datastore.add(new ArrayList<>()); 
     } 


     while ((line2 = read.readLine()) != null) { 
      String[] dataIn2 = line2.split(","); 

      if (cnt != 0) { 
       for (int i = 1; i <= dsize; i++) { 
        datastore.get(i - 1).add(dataIn2[i]); 
       } 
      } 
      cnt++; 
     } 

     for (String sn : switchNames) { 
      sections.add(new XYSeries(sn)); 
     } 

     JFreeChart xylineChart = ChartFactory.createXYLineChart(
       chartTitle, 
       "Category", 
       "Score", 
       createDataset(), 
       PlotOrientation.VERTICAL, 
       true, true, false); 

     ChartPanel chartPanel = new ChartPanel(xylineChart); 
     chartPanel.setPreferredSize(new java.awt.Dimension(1366, 768)); 
     final XYPlot plot = xylineChart.getXYPlot(); 
     XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer(); 

     plot.setRenderer(renderer); 
     setContentPane(chartPanel); 

    } 

    private XYDataset createDataset() { 


     ArrayList<Object> xystore = new ArrayList<>(); 

     for (String sn : switchNames) { 
      xystore.add(new XYSeries(sn)); 
     } 

     for (int i = 1; i <= switchNames.length; i++) { 
      for (String x : time) { 
       double y = Double.parseDouble(x); 
       ((XYSeries)xystore.get(i-1)).add(y, i); 
      } 
     } 

     final XYSeriesCollection dataset = new XYSeriesCollection(); 
     for (Object xy : xystore) { 
      dataset.addSeries((XYSeries)xy); 
     } 

     System.out.println(dataset); 
     return dataset; 

    } 

    public static String[] sNames(String[] names) { 
     String[] arr = Arrays.copyOfRange(names, 1, names.length); 
     return arr; 

    } 

    public static void DataClear() throws IOException, NullPointerException { 

    } 

    public static void main(String[] args) throws IOException { 

     DemoCSV test = new DemoCSV("Test", "second"); 

     test.pack(); 
     RefineryUtilities.centerFrameOnScreen(test); 
     test.setVisible(true); 
    } 
} 

当这个运行时,它产生的东西看起来像这样:

graph

我想改变点的颜色,当在CSV文件更改列中的值(无关紧要的颜色是什么,它的我们想看到的颜色变化我们看看它)。如果不可能,为每个节点设置值就足够了(具有悬停效果)。

+2

您可以覆盖'getItemPaint()',对于[示例](http://stackoverflow.com/search?tab=votes&q=user%3a230513%20%5bjfreechart%5d%20XYLineAndShapeRenderer%20getitempaint) 。 – trashgod

+0

谢谢你Trashgod。这是我正在搜索的内容,但现在我仍然坚持使用返回的颜色,我想要对齐每个系列的值。我认为我的问题是我无法找到调用节点颜色的代码。 (当调用getItemFillPaint时)。你能指出这个叫什么吗?因为我想根据“数据存储”arrayList中的值更改节点的颜色。这是我的代码到目前为止。 gist.github.com/Menuka5/e50c37fd7d4572c4966e –

+1

您可以使用[此处]显示的方法(http://stackoverflow.com/a/3006785/230513)查看默认值。请修改您的问题以包含显示您当前方法的[mcve]。 – trashgod

回答

1

这似乎不可能与XYPlot做到这一点。但是,您可以使用散点图(FastScatterPlot)来代替。不要将开关设置为系列,而应将值(TRUE,FALSE等)串联起来,并保持现在的X和Y位置。

此外,您应该为您的Y轴使用SymbolAxis。致电setRangeAxis在您的FastScatterPlot上使用创建的SymbolAxis

+0

我可以知道是否有你的答案的例子吗? –