All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.datastax.data.exploration.service.chart.impl.BrokenLineServiceImpl Maven / Gradle / Ivy

package com.datastax.data.exploration.service.chart.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.datastax.data.exploration.biz.datatable.DataTable;
import com.datastax.data.exploration.biz.datatable.DataType;
import com.datastax.data.exploration.biz.datatable.column.DataColumn;
import com.datastax.data.exploration.common.DataTypeHandler;
import com.datastax.data.exploration.common.File2DataTable;
import com.datastax.data.exploration.service.chart.BrokenLineService;
import com.datastax.data.exploration.util.Consts;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import static com.datastax.data.exploration.util.CommonUtil.getFormat;

/**
 * 折线图
 *
 * @author songfu 2017/12/15
 */

@Service("brokenLineService")
public class BrokenLineServiceImpl implements BrokenLineService {

    /**
     * 获取折线图初始化数据
     *
     * @param id 数据集id
     * @return 返回的是折线图初始化数据集
     */
    @Override
    public JSONObject brokenLineInit(String id) {
        List axisX = DataTypeHandler.noText(File2DataTable.tableSchema(id));
        if (axisX.size() == 0) {
            axisX.add(Consts.NONE);
        }
        JSONObject json = new JSONObject();
        json.put("axisX", axisX);
        json.put("axisY", axisX);
        return json;
    }

    /**
     * 获取折线图展示数据
     *
     * @param id     数据集id
     * @param axisX  折线图x轴
     * @param axisYs 折线图y轴
     * @return 返回的是折线图展示数据
     */
    @Override
    public JSONObject brokenLineValue(String id, String axisX, String axisYs) {
        List axisYList = JSONArray.parseArray(axisYs,String.class);
        if (axisX.equals(Consts.NONE)) {
            return new JSONObject();
        }
        DataTable table = File2DataTable.exactTable(id);
        JSONObject jsonX = getValue(table.getColumns().getColumn(axisX), axisX);
        List jsonYs = new ArrayList<>();
        for (String axisY : axisYList) {
            jsonYs.add(getValue(table.getColumns().getColumn(axisY), axisY));
        }
        JSONObject json = new JSONObject();
        json.put("axisX", jsonX);
        json.put("axisY", jsonYs);
        return json;
    }

    private static JSONObject getValue(DataColumn dataColumn, String column) {
        DataType dataType = dataColumn.getType();
        JSONObject json = new JSONObject();
        json.put("name", column);
        json.put("type", dataType.getName());
        json.put("format", getFormat(dataType));
        json.put("data", dataColumn.getNotNullValues());
        return json;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy