当前位置: 首页 > 工具软件 > ECharts-Java > 使用案例 >

echarts-java类库K线图例子

江衡
2023-12-01
package com.resset.datav.cache;

import java.sql.Timestamp;
import java.util.List;

import com.github.abel533.echarts.AxisPointer;
import com.github.abel533.echarts.DataZoom;
import com.github.abel533.echarts.Grid;
import com.github.abel533.echarts.Title;
import com.github.abel533.echarts.Tooltip;
import com.github.abel533.echarts.axis.AxisLine;
import com.github.abel533.echarts.axis.CategoryAxis;
import com.github.abel533.echarts.axis.SplitArea;
import com.github.abel533.echarts.axis.SplitLine;
import com.github.abel533.echarts.axis.ValueAxis;
import com.github.abel533.echarts.code.Align;
import com.github.abel533.echarts.code.AxisType;
import com.github.abel533.echarts.code.DataZoomType;
import com.github.abel533.echarts.code.Magic;
import com.github.abel533.echarts.code.Orient;
import com.github.abel533.echarts.code.PointerType;
import com.github.abel533.echarts.code.Position;
import com.github.abel533.echarts.code.Tool;
import com.github.abel533.echarts.code.Trigger;
import com.github.abel533.echarts.code.X;
import com.github.abel533.echarts.data.KData;
import com.github.abel533.echarts.feature.MagicType;
import com.github.abel533.echarts.json.GsonOption;
import com.github.abel533.echarts.series.Candlestick;
import com.github.abel533.echarts.series.Line;
import com.github.abel533.echarts.style.TextStyle;
import com.resset.datav.util.DateUtils;

/*
 * K线图生成
 */
public class KCharts {

	/*
	 * K线图 KData类:为k线图的数据格式设置
	 * titleName:标题名称
	 * legName-4:分布对应legend五个分类图表
	 * vDatas:为x轴名称集合
	 * yDatas1-4:对应k线的开盘收盘最低最高价数据集合
	 */
	public static GsonOption kChart(String titleName,String legName,String legName1,String legName2,String legName3,String legName4, List vDatas, List yDatas1,List yDatas2,
			List yDatas3,List yDatas4) {
		GsonOption option = new GsonOption();

		Title title = new Title();
		title.text(titleName).setTextAlign(X.left);
		option.title(title);
		option.legend().data(legName,legName1,legName2,legName3,legName4).itemHeight(10).itemWidth(10).orient(Orient.vertical)
		.textStyle(new TextStyle().fontSize(10)).right(1).top(30);
		option.tooltip(new Tooltip().position(Position.right).transitionDuration(5.0).trigger(Trigger.axis)
				.axisPointer(new AxisPointer().type(PointerType.cross)).position(Position.top)
				.transitionDuration(500.0));
		option.toolbox().show(true).feature(Tool.mark, Tool.dataView, new MagicType(Magic.line, Magic.bar).show(true),
				Tool.restore, Tool.saveAsImage);
		option.calculable(true);
		option.grid(new Grid().left("10%").right("10%").bottom("15%"));

		CategoryAxis valueAxis = new CategoryAxis();
		valueAxis.type(AxisType.category).scale(true).boundaryGap(false).axisLine(new AxisLine().onZero(false))
				.splitLine(new SplitLine().show(false)).min("dataMin").max("dataMax");
		if (vDatas.size() > 0) {
			if (vDatas.get(0) instanceof Timestamp) {
				for (int i = 0; i < vDatas.size(); i++) {
					if (vDatas.get(i) != null) {
						valueAxis.data(DateUtils.currentDate(vDatas.get(i)));
					}
				}
			} else {
				for (int i = 0; i < vDatas.size(); i++) {
					if (vDatas.get(i) != null) {
						valueAxis.data(vDatas.get(i).toString());
					}
				}
			}
		}
		option.xAxis(valueAxis);

		option.dataZoom(new DataZoom().type(DataZoomType.inside).start(50).end(100),
				new DataZoom().show(true).type(DataZoomType.slider).y("90%").start(50).end(100));

		option.yAxis(new ValueAxis().scale(true).splitArea(new SplitArea().show(true)));

		Candlestick candlestick = new Candlestick(legName);
		
		//k线图数据赋值,最好有四组数据,分别为 :第一个数值:开盘 第二个数值:收盘 第三个数值:最低值 第四个数值:最高值
		if (yDatas1.size() > 0 && yDatas2.size() >0 && yDatas3.size() >0 && yDatas4.size() >0) {
			for (int i = 0; i < yDatas1.size();i++ ) {
				//为null与""做默认数据添加,防止出现异常
				if (yDatas1.get(i) != null && yDatas2.get(i) != null && yDatas3.get(i) != null && yDatas4.get(i) != null) {
					if (!yDatas1.get(i).equals("") && !yDatas2.get(i).equals("") && !yDatas3.get(i).equals("") && !yDatas4.get(i).equals("")) {
						candlestick.data(new KData(Double.valueOf(yDatas1.get(i).toString()),Double.valueOf(yDatas2.get(i).toString())
								,Double.valueOf(yDatas3.get(i).toString()),Double.valueOf(yDatas4.get(i).toString())));
					}else {
						candlestick.data(new KData(0.0,0.0,0.0,0.0));
					}
				}
			}
			}else {
				candlestick.data(new KData(0.0,0.0,0.0,0.0));
			}
		
		
		Line line =new Line(legName1);
		for (int i = 0; i < yDatas1.size(); i++) {
			line.smooth(true).data(yDatas1.get(i));
		}
		Line line2 =new Line(legName2);
		for (int i = 0; i < yDatas2.size(); i++) {
			line2.smooth(true).data(yDatas2.get(i));
		}
		Line line3 =new Line(legName3);
		for (int i = 0; i < yDatas3.size(); i++) {
			line3.smooth(true).data(yDatas3.get(i));
		}
		Line line4 =new Line(legName4);
		for (int i = 0; i < yDatas4.size(); i++) {
			line4.smooth(true).data(yDatas4.get(i));
		}
		option.series(candlestick,line,line2,line3,line4);

		return option;
	}

	private static int random() {
		int i = (int) Math.round(Math.random() * 100);
		return i;
	}

	/*
	 * 临时数据 第一个数值:开盘 第二个数值:收盘 第三个数值:最低值 第四个数值:最高值
	 */
	private static KData[] randomDataArray1() {
		KData[] scatters = new KData[85];
		for (int i = 0; i < scatters.length; i++) {
			scatters[i] = new KData(Double.valueOf(random()), Double.valueOf(random()), Double.valueOf(random()),
					Double.valueOf(random()));
		}
		return scatters;
	}
}
mavn资源jar
<dependency>
	<groupId>com.github.abel533</groupId>
	<artifactId>ECharts</artifactId>
	<version>3.0.0</version>
</dependency>

 类似资料: