Java 基于类别地块的y轴更改散点图的颜色
如何基于Java 基于类别地块的y轴更改散点图的颜色,java,jfreechart,Java,Jfreechart,如何基于类别地块的y轴更改散点图的颜色。我已经包含了创建图表的以下代码: package plot; import java.awt.Color; import java.awt.Dimension; import java.awt.Paint; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.Map; import javax.swing.JPanel; import org.jfree.
类别地块
的y轴更改散点图的颜色。我已经包含了创建图表的以下代码:
package plot;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Paint;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import javax.swing.JPanel;
import org.jfree.chart.ChartFactory;
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.PlotOrientation;
import org.jfree.chart.plot.ValueMarker;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;
import org.jfree.util.ShapeUtilities;
import com.orsoncharts.marker.Marker;
public class Plot extends ApplicationFrame {
private static final long serialVersionUID = 1L;
public Plot(String s) {
super(s);
JPanel jpanel = createDemoPanel();
jpanel.setPreferredSize(new Dimension(500, 270));
setContentPane(jpanel);
}
private static CategoryDataset createDataset() {
HashMap<String, Integer> hmap1 = new HashMap<String, Integer>();
hmap1.put("S1", 6);
hmap1.put("S2", 4);
hmap1.put("S3", 5);
hmap1.put("S4", 3);
hmap1.put("S5", 2);
DefaultCategoryDataset defaultcategorydataset = new DefaultCategoryDataset();
for (Map.Entry<String, Integer> entry : hmap.entrySet()) {
String key = entry.getKey();
Integer value = entry.getValue();
defaultcategorydataset.addValue(value, "Graph", key);
}
return defaultcategorydataset;
}
private static JFreeChart createChart(final CategoryDataset categorydataset) {
int up = 9;
int low = 2;
int mid = 5;
JFreeChart jfreechart = ChartFactory.createLineChart("", "X", "Y", categorydataset, PlotOrientation.VERTICAL, true, true, false);
jfreechart.setBackgroundPaint(Color.white);
CategoryPlot categoryplot = (CategoryPlot) jfreechart.getPlot();
categoryplot.setBackgroundPaint(Color.lightGray);
categoryplot.setRangeGridlinePaint(Color.lightGray);
NumberAxis numberaxis = (NumberAxis) categoryplot.getRangeAxis();
numberaxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
numberaxis.setLowerBound(0);
numberaxis.setUpperBound(up+1);
ValueMarker upper = new ValueMarker(up);
rupper.setPaint(Color.BLUE);
categoryplot.addRangeMarkerrupper);
ValueMarker lower = new ValueMarker(low);
yupper.setPaint(Color.YELLOW);
categoryplot.addRangeMarker(lower);
ValueMarker middle = new ValueMarker(mid);
mdefault.setPaint(Color.RED);
categoryplot.addRangeMarker(middle);
LineAndShapeRenderer lineandshaperenderer = new LineAndShapeRenderer(){
@Override
public Paint getItemPaint(int row, int col) {
Paint cpaint = getItemColor(row, col);
if (cpaint == null) {
cpaint = super.getItemPaint(row, col);
}
return cpaint;
}
public Color getItemColor(int row, int col) {
// System.out.println(col + "," + dataset.getY(row, col));
double y = (Double) categorydataset.getColumnKey(col);
if(y<=low | y > up) return Color.black;
if(y<=mid) return Color.green;
if((y<=up | y>low)& ( y != mid))return Color.yellow;
return null;
}
};
lineandshaperenderer = (LineAndShapeRenderer) categoryplot.getRenderer();
lineandshaperenderer.setItemMargin(0);
lineandshaperenderer.setSeriesShapesVisible(0, true);
lineandshaperenderer.setSeriesLinesVisible(0, false);
return jfreechart;
}
public static JPanel createDemoPanel() {
JFreeChart jfreechart = createChart(createDataset());
return new ChartPanel(jfreechart);
}
public static void main(String args[]) {
Plot plot= new Plot("Plot");
plot.pack();
RefineryUtilities.centerFrameOnScreen(plot);
plot.setVisible(true);
}
}
包图;
导入java.awt.Color;
导入java.awt.Dimension;
导入java.awt.Paint;
导入java.util.HashMap;
导入java.util.LinkedHashMap;
导入java.util.Map;
导入javax.swing.JPanel;
导入org.jfree.chart.ChartFactory;
导入org.jfree.chart.ChartPanel;
导入org.jfree.chart.JFreeChart;
导入org.jfree.chart.axis.CategoryAxis;
导入org.jfree.chart.axis.CategoryLabelPositions;
导入org.jfree.chart.axis.NumberAxis;
导入org.jfree.chart.axis.ValueAxis;
导入org.jfree.chart.plot.CategoryPlot;
导入org.jfree.chart.plot.PlotOrientation;
导入org.jfree.chart.plot.ValueMarker;
导入org.jfree.chart.renderer.category.LineAndShapeRenderer;
导入org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
导入org.jfree.data.category.CategoryDataset;
导入org.jfree.data.category.DefaultCategoryDataset;
导入org.jfree.ui.ApplicationFrame;
导入org.jfree.ui.RefineryUtilities;
导入org.jfree.util.ShapeUtilities;
导入com.orsoncharts.marker.marker;
公共类Plot扩展了ApplicationFrame{
私有静态最终长serialVersionUID=1L;
公共绘图(字符串s){
超级(s);
JPanel JPanel=createDemoPanel();
jpanel.setPreferredSize(新尺寸(500270));
setContentPane(jpanel);
}
私有静态CategoryDataset createDataset(){
HashMap hmap1=新的HashMap();
hmap1.put(“S1”,6);
hmap1.put(“S2”,4);
hmap1.put(“S3”,5);
hmap1.put(“S4”,3);
hmap1.put(“S5”,2);
DefaultCategoryDataset DefaultCategoryDataset=新的DefaultCategoryDataset();
对于(Map.Entry:hmap.entrySet()){
String key=entry.getKey();
整数值=entry.getValue();
defaultcategorydataset.addValue(值,“图形”,键);
}
返回defaultcategorydataset;
}
私有静态JFreeChart createChart(最终类别数据集类别数据集){
int up=9;
int低=2;
int-mid=5;
JFreeChart JFreeChart=ChartFactory.createLineChart(“,“X”,“Y”,类别数据集,PlotOrientation.VERTICAL,true,true,false);
jfreechart.setBackgroundPaint(颜色:白色);
CategoryPlot CategoryPlot=(CategoryPlot)jfreechart.getPlot();
categoryplot.setBackgroundPaint(颜色:浅灰色);
categoryplot.setRangeGridlinePaint(颜色:浅灰色);
NumberAxis NumberAxis=(NumberAxis)categoryplot.getRangeAxis();
numberaxis.setstandarddickUnits(numberaxis.createIntegerTickUnits());
数字轴setLowerBound(0);
数字轴设置上限(向上+1);
ValueMarker upper=新的ValueMarker(向上);
rupper.setPaint(颜色:蓝色);
categoryplot.AddRangeMarkerUpper);
ValueMarker lower=新的ValueMarker(低);
yupper.setPaint(颜色:黄色);
categoryplot.addRangeMarker(下部);
ValueMarker中间=新的ValueMarker(中间);
mdefault.setPaint(颜色:红色);
categoryplot.addRangeMarker(中间);
LineandShaperEnder LineandShaperEnder=新的LineandShaperEnder(){
@凌驾
公共绘制getItemPaint(int行,int列){
Paint cpaint=getItemColor(行、列);
if(cpaint==null){
cpaint=super.getItemPaint(行、列);
}
返回cpaint;
}
公共颜色getItemColor(int行,int列){
//System.out.println(col+”,“+dataset.getY(row,col));
双y=(双)categorydataset.getColumnKey(列);
如果(y向上)返回Color.black;
如果(y在修复语法错误和删除无关导入后,仍存在以下问题:
- 使用自定义渲染器,而不是由
图表工厂创建的渲染器:
//lineandshaperenderer = (LineAndShapeRenderer) categoryplot.getRenderer();
…
categoryplot.setRenderer(lineandshaperenderer);
- 使用数据集的值作为判别标准:
int y = categorydataset.getValue(row, col).intValue();
运行时的代码:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Paint;
import java.util.HashMap;
import java.util.Map;
import javax.swing.JPanel;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.ValueMarker;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;
public class Plot extends ApplicationFrame {
private static final long serialVersionUID = 1L;
public Plot(String s) {
super(s);
JPanel jpanel = createDemoPanel();
jpanel.setPreferredSize(new Dimension(500, 270));
setContentPane(jpanel);
}
private static CategoryDataset createDataset() {
HashMap<String, Integer> hmap1 = new HashMap<String, Integer>();
hmap1.put("S1", 6);
hmap1.put("S2", 4);
hmap1.put("S3", 5);
hmap1.put("S4", 3);
hmap1.put("S5", 2);
DefaultCategoryDataset defaultcategorydataset = new DefaultCategoryDataset();
for (Map.Entry<String, Integer> entry : hmap1.entrySet()) {
String key = entry.getKey();
Integer value = entry.getValue();
defaultcategorydataset.addValue(value, "Graph", key);
}
return defaultcategorydataset;
}
private static JFreeChart createChart(final CategoryDataset categorydataset) {
int up = 9;
int low = 2;
int mid = 5;
JFreeChart jfreechart = ChartFactory.createLineChart("", "X", "Y",
categorydataset, PlotOrientation.VERTICAL, true, true, false);
jfreechart.setBackgroundPaint(Color.white);
CategoryPlot categoryplot = (CategoryPlot) jfreechart.getPlot();
categoryplot.setBackgroundPaint(Color.lightGray);
categoryplot.setRangeGridlinePaint(Color.lightGray);
NumberAxis numberaxis = (NumberAxis) categoryplot.getRangeAxis();
numberaxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
numberaxis.setLowerBound(0);
numberaxis.setUpperBound(up + 1);
ValueMarker upper = new ValueMarker(up);
upper.setPaint(Color.BLUE);
categoryplot.addRangeMarker(upper);
ValueMarker lower = new ValueMarker(low);
lower.setPaint(Color.YELLOW);
categoryplot.addRangeMarker(lower);
ValueMarker middle = new ValueMarker(mid);
middle.setPaint(Color.RED);
categoryplot.addRangeMarker(middle);
LineAndShapeRenderer lineandshaperenderer = new LineAndShapeRenderer() {
@Override
public Paint getItemPaint(int row, int col) {
System.out.println(row + ", " + col);
Paint cpaint = getItemColor(row, col);
if (cpaint == null) {
cpaint = super.getItemPaint(row, col);
}
return cpaint;
}
public Color getItemColor(int row, int col) {
int y = categorydataset.getValue(row, col).intValue();
if (y <= low | y > up) {
return Color.black;
}
if (y <= mid) {
return Color.green;
}
if ((y <= up | y > low) & (y != mid)) {
return Color.yellow;
}
return null;
}
};
lineandshaperenderer.setItemMargin(0);
lineandshaperenderer.setSeriesShapesVisible(0, true);
lineandshaperenderer.setSeriesLinesVisible(0, false);
categoryplot.setRenderer(lineandshaperenderer);
return jfreechart;
}
public static JPanel createDemoPanel() {
JFreeChart jfreechart = createChart(createDataset());
return new ChartPanel(jfreechart);
}
public static void main(String args[]) {
Plot plot = new Plot("Plot");
plot.pack();
RefineryUtilities.centerFrameOnScreen(plot);
plot.setVisible(true);
}
}
导入java.awt.Color;
导入java.awt.Dimension;
导入java.awt.Paint;
导入java.util.HashMap;
导入java.util.Map;
导入javax.swing.JPanel;
导入org.jfree.chart.ChartFactory;
导入org.jfree.chart.ChartPanel;
导入org.jfree.chart.JFreeChart;
导入org.jfree.chart.axis.NumberAxis;
导入org.jfree.chart.plot.CategoryPlot;
导入org.jfree.chart.plot.PlotOrientation;
导入org.jfree.chart.plot.ValueMarker;
导入org.jfree.chart.renderer.category.LineAndShapeRenderer;
导入org.jfree.data.category.CategoryDataset;
导入org.jfree.data.category.DefaultCategoryDataset;
导入org.jfree.ui.ApplicationFrame;
导入org.jfree.ui.RefineryUtilities;
公共类Plot扩展了ApplicationFrame{
私有静态最终长serialVersionUID=1L;
公共绘图(字符串s){
超级(s);
JPanel JPanel=createDemoPanel();
jpanel.setPreferredSize(新尺寸(500270));
setContentPane(jpanel);
}
私有静态CategoryDataset createDataset(){
HashMap hmap1=新的HashMap();
hmap1.put(“S1”,6);
hmap1.put(“S2”,4);
hmap1.put(“S3”,5);
hmap1.put(“S4”,3);
hmap1.put(“S5”,2);
DefaultCategoryDataset DefaultCategoryDataset=新的DefaultCategoryDataset();
对于(Map.Entry:hmap1.entrySet()){
String key=entry.getKey();
整数值=entry.getValue();
defaultcategorydataset.addValue(值,“图形”,键);
}
返回defaultcategorydataset;
}
私有静态JFreeChart createChart(最终类别数据集类别数据集){
int up=9;
int低=2;
int-mid=5;
JFreeChart JFreeChart=ChartFactory.createLineChart(“,”X“,”Y“,
categorydataset,PlotOrientation.VERTICAL,true,true,false);
jfreechart.setBackgroundPaint(颜色:w