package com.sipai.jspatrol.PointAnalysis;

import com.github.abel533.echarts.DataZoom;
import com.github.abel533.echarts.Grid;
import com.github.abel533.echarts.Legend;
import com.github.abel533.echarts.Tooltip;
import com.github.abel533.echarts.axis.AxisLabel;
import com.github.abel533.echarts.axis.CategoryAxis;
import com.github.abel533.echarts.axis.ValueAxis;
import com.github.abel533.echarts.code.DataZoomType;
import com.github.abel533.echarts.code.Orient;
import com.github.abel533.echarts.code.Trigger;
import com.github.abel533.echarts.json.GsonOption;
import com.github.abel533.echarts.series.Line;
import com.github.abel533.echarts.series.Series;
import com.github.abel533.echarts.style.TextStyle;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class EchartsOptionUtil {

    public static GsonOption getLineChartOptions(String name,Object[] xAxis, Object[] yAxis) {
        GsonOption option = new GsonOption();
        //option.title().setText("");
        //图例
        option.legend(name);
        //trigger为'item'时只会显示该点的数据，为'axis'时显示该列下所有坐标轴所对应的数据
        Tooltip tooltip = new Tooltip();
        TextStyle textStyle = new TextStyle();
        textStyle.setFontSize(12);
        tooltip.setTextStyle(textStyle);
        tooltip.setPadding(5);
        tooltip.setWidth(220);
        option.setTooltip(tooltip);
        option.tooltip().trigger(Trigger.axis);
        //设置y轴数据
        ValueAxis valueAxis = new ValueAxis();
        option.yAxis(valueAxis);
        CategoryAxis categorxAxis = new CategoryAxis();
        categorxAxis.axisLine().onZero(false);
        categorxAxis.boundaryGap(true);
        //设置x轴数据
        categorxAxis.data(xAxis);
        option.xAxis(categorxAxis);
        Grid grid = new Grid();
        grid.left(50);
        option.setGrid(grid);
        Line line = new Line();
        //注意！！name要和上面的图例名称一样，不然图例会不显示！！
        line.smooth(false).name(name).symbol("none").data(yAxis).itemStyle().normal().lineStyle().shadowColor("rgba(0,0,0,0.4)");
        option.series(line);
        List<DataZoom> zooms = new ArrayList<>();
        DataZoom zoom = new DataZoom();
        zoom.setType(DataZoomType.slider);
        zooms.add(zoom);
        option.setDataZoom(zooms);
        return option;
    }

    public static GsonOption getLinesChartOptions(String name,String name1,Object[] xAxis, Object[] yAxis,Object[] y2Axis) {
        GsonOption option = new GsonOption();
        //option.title().setText("");
        //图例
        Object[] names = new Object[2];
        names[0] = name;
        names[1] = name1;
        option.legend(names);
        //trigger为'item'时只会显示该点的数据，为'axis'时显示该列下所有坐标轴所对应的数据
        option.tooltip().trigger(Trigger.axis);
        Legend legend = new Legend();
        legend.setOrient(Orient.horizontal);
        option.setLegend(legend);
        //设置y轴数据
        ValueAxis valueAxis = new ValueAxis();
        option.yAxis(valueAxis);
        CategoryAxis categorxAxis = new CategoryAxis();
        categorxAxis.axisLine().onZero(false);
        categorxAxis.boundaryGap(true);
        //设置x轴数据
        categorxAxis.data(xAxis);
        option.xAxis(categorxAxis);
        List<Series> seriesList = new ArrayList<>();
        Line line = new Line();
        //注意！！name要和上面的图例名称一样，不然图例会不显示！！
        line.smooth(false).name(names[0].toString()).symbol("none").data(yAxis).itemStyle().normal().lineStyle().shadowColor("rgba(0,0,0,0.4)");
        Line line1 = new Line();
        line1.smooth(false).name(names[1].toString()).symbol("none").data(y2Axis).itemStyle().normal().color("#BE55F5");
        seriesList.add(line);
        seriesList.add(line1);
        option.series(seriesList);
        List<DataZoom> zooms = new ArrayList<>();
        DataZoom zoom = new DataZoom();
        zoom.setType(DataZoomType.slider);
        zooms.add(zoom);
        option.setDataZoom(zooms);
        return option;
    }

    public static GsonOption getListLinesChartOptions(List<String> nameList,Object[] xAxis, List<Object[]> yAxisList) {
        GsonOption option = new GsonOption();
        //option.title().setText("");
        //图例
        Object[] names = new Object[nameList.size()];
        for (int a=0;a<nameList.size();a++){
            names[a] = nameList.get(a);
        }
        option.legend(names);
        //trigger为'item'时只会显示该点的数据，为'axis'时显示该列下所有坐标轴所对应的数据
        Tooltip tooltip = new Tooltip();
        TextStyle textStyle = new TextStyle();
        textStyle.setFontSize(12);
        tooltip.setTextStyle(textStyle);
        tooltip.setPadding(5);
        tooltip.setWidth(220);
        option.setTooltip(tooltip);
        option.tooltip().trigger(Trigger.axis);
        //设置y轴数据
        ValueAxis valueAxis = new ValueAxis();
        option.yAxis(valueAxis);
        CategoryAxis categorxAxis = new CategoryAxis();
        categorxAxis.axisLine().onZero(false);
        categorxAxis.boundaryGap(true);
        //设置x轴数据
        categorxAxis.data(xAxis);
        AxisLabel axisLabel = new AxisLabel();
        TextStyle xtextStyle = new TextStyle();
        xtextStyle.setFontSize(8);
        axisLabel.margin(5);
        axisLabel.setTextStyle(xtextStyle);
        categorxAxis.setAxisLabel(axisLabel);
        option.xAxis(categorxAxis);
        List<Series> seriesList = new ArrayList<>();
        Random random = new Random();
        for (int i=0;i<yAxisList.size();i++){
            String colorCode = "#" + String.format("%02X", random.nextInt(256)) +
                    String.format("%02X", random.nextInt(256)) +
                    String.format("%02X", random.nextInt(256));
            Line line = new Line();
            //注意！！name要和上面的图例名称一样，不然图例会不显示！！
            line.smooth(false).name(names[i].toString()).symbol("none").data(yAxisList.get(i)).itemStyle().normal().color(colorCode);
            seriesList.add(line);
        }
        option.series(seriesList);
        Grid grid = new Grid();
        grid.left(50);
        option.setGrid(grid);
        List<DataZoom> zooms = new ArrayList<>();
        DataZoom zoom = new DataZoom();
        zoom.setType(DataZoomType.slider);
        zooms.add(zoom);
        option.setDataZoom(zooms);
        return option;
    }
}
