java - (highCharts)饼图生成及美化

一、基本饼图

参考Highcharts官网.
本次我们来介绍一下如何通过封装的highcharts jar包,来实现java端饼图的生成及美化。

1、效果示例

1

2、java代码实现

2.1 饼图封装类

import org.moxieapps.gwt.highcharts.client.Chart;
import org.moxieapps.gwt.highcharts.client.Exporting;
import org.moxieapps.gwt.highcharts.client.Legend;
import org.moxieapps.gwt.highcharts.client.Legend.Align;
import org.moxieapps.gwt.highcharts.client.Point;
import org.moxieapps.gwt.highcharts.client.Series;
import org.moxieapps.gwt.highcharts.client.ToolTip;
import org.moxieapps.gwt.highcharts.client.labels.PieDataLabels;
import org.moxieapps.gwt.highcharts.client.plotOptions.PiePlotOptions;
import org.moxieapps.gwt.highcharts.client.plotOptions.PlotOptions.Cursor;
import com.google.gwt.user.client.ui.IsWidget;
import com.google.gwt.user.client.ui.Widget;
/** 饼图封装类*/
public class PieChart implements IsWidget{
	protected Chart chart = null;
	int W;int H;
	public PieChart(int w, int h) {
		W = w; H = h;
	}
	public Chart createChart(String chartName, String xName, String yName) {
		chart = new Chart()
				.setSize(W, H)
				.setType(Series.Type.PIE)
				.setMargin(40, 40, 50, 60)
				.setChartTitleText(chartName)
				.setExporting(new Exporting().setEnabled(false))
				.setLegend(new Legend().setAlign(Align.CENTER));
		chart.setPiePlotOptions(new PiePlotOptions()
				.setAllowPointSelect(true)
				.setCursor(Cursor.POINTER)
				.setShowInLegend(true)
				.setPieDataLabels(new PieDataLabels()
						.setEnabled(true)
						.setFormat("<b>{point.name}</b>: {point.percentage:.1f} %")));
		chart.setToolTip(new ToolTip().setPointFormat("{series.name}: <b>{point.percentage:.1f}%</b>"));
		return chart;
	}
	/** 添加扇形区 */
	public Series addSeries(Point[] points, String name,String color){
		Series s = chart.createSeries()
						.setPoints(points)
						.setName(name);
		chart.addSeries(s);
		return s;
	}
	@Override
	public Widget asWidget() {
		return chart;
	}
}

2.2 调用类

import org.moxieapps.gwt.highcharts.client.Point;
import com.math.ezone.ezuimodel.client.splinechart.PieChart;
import com.sencha.gxt.widget.core.client.Window;
public class PieChartWindow extends Window{
	public static final long hour = 60*60*1000;
	public static final long day = hour*24;
	private int w,h;
	private PieChart sc;
	public PieChartWindow(int w,int h) {
		this.w = w;
		this.h = h;
		this.setPixelSize(w, h);
	}
	public PieChartWindow createChat(){
		this.setTitle("测试饼图生成");
		this.setHeadingText("测试饼图生成");
		sc = new PieChart(w, h-40);
		//可传入实际数据-这里仅用于测试
		sc.createChart("测试饼图生成", "x轴数据", "y轴数据");
		this.add(sc);
		addSeries();
		return this;
	}
	/**构造测试数据-并添加扇形区*/
	public void addSeries(){
		//构造数据-》实际可通过传入数据添加
		Point[] points = new Point[6];
		points[0] = new Point(0, 30).setName("苹果").setSliced(true).setSelected(true);
		points[1] = new Point(0, 20).setName("梨子").setSliced(true).setSelected(true);
		points[2] = new Point(0, 40).setName("香蕉").setSliced(true).setSelected(true);
		points[3] = new Point(0, 10).setName("橙子").setSliced(true).setSelected(true);
		points[4] = new Point(0, 15).setName("柚子").setSliced(true).setSelected(true);
		points[5] = new Point(0, 12).setName("荔枝").setSliced(true).setSelected(true);
		sc.addSeries(points, "水果占比","");
	}
}

二、标题居中的环形图

1 效果示例

2

2、代码封装

2.1中对应createChart()函数,封装为createRingChart()如下所示:

public Chart createRingChart(String chartName, String xName, String yName) {
		chart = new Chart()
				.setSize(W, H)
				.setType(Series.Type.PIE)
				.setMargin(40, 40, 50, 60)
				.setChartTitleText(chartName)
				.setExporting(new Exporting().setEnabled(false))
				.setLegend(new Legend().setAlign(Align.CENTER));
		chart.setPiePlotOptions(new PiePlotOptions()
				.setAllowPointSelect(true)
				.setCursor(Cursor.POINTER)
				.setShowInLegend(true)
				.setPieDataLabels(new PieDataLabels()
						.setEnabled(true)
						.setFormat("<b>{point.name}</b>: {point.percentage:.1f} %")));
		chart.setToolTip(new ToolTip().setPointFormat("{series.name}: <b>{point.percentage:.1f}%</b>"));
		return chart;
	}

然后修改2.2.中的调用即可

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Highcharts饼图的配置项包括以下内容: 1. `legend`:图例的设置,可以控制图例的显示、布局、背景颜色等。 2. `plotOptions`:绘图选项,可以设置饼图的大小、内圈直径等。 3. `tooltip`:提示框的设置,可以定义提示框的格式。 4. `chart`:图表的设置,包括类型、背景颜色、边距等。 5. `title`:标题的设置。 6. `series`:数据列的设置,可以定义饼图的数据以及数据标签的样式。 这些配置项可以通过Javascript对象的方式进行设置,例如: ``` let option = { chart: { type: "pie", backgroundColor: "rgba(0, 0, 0, 0)", marginTop:10, options3d: { enabled: true, alpha: 50, beta: 0, }, }, title: { text: "", }, plotOptions: { pie: { borderColor: "#000", borderWidth: 1, allowPointSelect: true, cursor: "pointer", depth: 24, dataLabels: { enabled: true, format: "{point.name}", style: { textOutline: "none", color: "rgba(45,153,255,1)", fontSize: 11, }, }, }, }, series: [ { type: "pie", data: [ ["邮政包裹", 30], ["圆通快递", 20], ["京东快递", 30], ["申通快递", 50], ["德邦物流", 20], ["天天快递", 20], ["其他", 30], ["顺丰快递", 10], ["中通快递", 30], ["韵达快递", 20], ], }, ], }; Highcharts.chart("DispatchProportion", option); ``` 以上就是绘制Highcharts饼图所需的配置项。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [Highcharts 3D饼图(1)上手使用配置](https://blog.csdn.net/kkk_xxx/article/details/109380972)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [highcharts 饼图常用设置项](https://blog.csdn.net/qq_26769677/article/details/80229195)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值