JFChart自定义柱体颜色(可自定义每个柱体为不同的颜色)

一、实现:添加Renderer.java,继承BarRenderer.java

 

 

package com.gllic.workweixin.mqdto;

import java.awt.*;

/**
* @program: workweixin-mobile-api
* @ClassName Renderer
* @description: 初始化JFreeChart柱体颜色
* @author: Marlo
* @create: 2021-06-24 15:04
* @Version 1.0
**/
public class Renderer extends org.jfree.chart.renderer.category.BarRenderer{

/**
*
*/
private static final long serialVersionUID = 784630226449158436L;
private Paint[] colors;
//初始化柱子颜色
//private String[] colorValues = { "#FE5F63", "#4AD07C", "#FDCB54", "#FDCB54", "#FDCB54", "#FDCB54", "#FDCB54", "#FDCB54", "#FDCB54", "#FDCB54" };

public Renderer(String[] colorValues) {
if(colorValues.length==0){
colorValues = new String[]{ "#21C3CA" };
}
colors = new Paint[colorValues.length];
for (int i = 0; i < colorValues.length; i++) {
colors[i] = Color.decode(colorValues[i]);
}
}

//每根柱子以初始化的颜色不断轮循
public Paint getItemPaint(int i, int j) {
return colors[j % colors.length];
}
}

二、调用:
//自定义颜色数组,根据数组内颜色循环显示柱体
String[] colorValues = {"#2197FF", "#7CCC48", "#21C3CA", "#21C3CA", "#21C3CA", "#21C3CA", "#21C3CA", "#21C3CA", "#21C3CA", "#21C3CA"};
BarRenderer renderer = new Renderer(colorValues);
上一篇:《图解Vue3.0》- 第10节 组件注册与使用


下一篇:Java编程入门与应用 P100—例4-8 (使用split()方法对字符串进行分割)