chart.san 5.7 KB
Newer Older
B
BingBlog 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
<template>
    <div class="visual-dl-charts">
        <div class="visual-dl-chart-box" style="{{computedStyle}}">
        </div>
        <div class="visual-dl-chart-actions">
            <ui-dropdown-menu
                hintText="download type"
                items="{{runsItems}}"
                value="{=downloadType=}"
            />
            <san-button on-click="handleDownLoad">
                <san-icon>file_download</san-icon>
            </san-button>
        </div>
    </div>
</template>
<script>
import Button from 'san-mui/Button';
import Icon from 'san-mui/Icon';
import DropDownMenu from '../../../common/ui/DropDownMenu';
import {generateJsonAndDownload} from '../../../common/fun/downLoadFile';
import echarts from 'echarts';
import axios from 'axios';
import {getPluginScalarsScalars} from '../../../service';
export default {
    components: {
        'ui-dropdown-menu': DropDownMenu,
        'san-button': Button,
        'san-icon': Icon
    },
    computed: {
        computedStyle() {
            let width = this.data.get('width');
            let height = this.data.get('height');
            return 'height:' + height + 'px;'
                + 'width:' + width + 'px;';
        }
    },
    initData() {
        return {
            width: 400,
            height: 300,
            // line config
            options: {},
            data: [
                {
                    name: 'train',
                    value: []
                }
            ],
            // choose run type for download file
            downloadType: ''
        };
    },
    inited() {
        this.watch('runsItems', val => {
            this.initDownloadType();
        });
    },

    attached() {
        let tagInfo = this.data.get('tagInfo');
        this.initCharts(tagInfo);
    },

    initDownloadType() {
        let runsItems = this.data.get('runsItems') || [];
        if (runsItems.length === 0) {
            return;
        }
        this.data.set('downloadType', runsItems.find((item, index) => index === 0).value);
    },

    initCharts(tagInfo) {
        this.createChart();
        this.setChartsOptions(tagInfo);
        this.setChartsData(tagInfo);
    },

    createChart() {
        let el = this.el.getElementsByClassName('visual-dl-chart-box')[0];
        this.myChart = echarts.init(el);
    },
    setChartsOptions({tagList, tag}) {
        let seriesOption = tagList.map(item => {
            return {
                name: item.run,
                type: 'line',
                showSymbol: false,
                hoverAnimation: false,
                data: [],
                smooth: true
            };
        });
        let legendOptions = tagList.map(item => item.run);
        let option = {
            title: {
                text: tag,
                textStyle: {
                    fontSize: '12'
                }
            },
            tooltip: {
                trigger: 'axis',
                axisPointer: {
                    animation: false
                },
                position: [10, 300]
            },
            toolbox: {
                show: true,
                showTitle: true,
                feature: {
                    dataZoom: {},
                    restore: {},
                    saveAsImage: {}
                },
                left: 40,
                top: 270
            },
            legend: {
                data: legendOptions,
                top: 30
            },
            grid: {
                top: 70,
                bottom: 50
            },
            xAxis: [
                {
                    type: 'value',
                    boundaryGap: false
                },
                {
                    type: 'value',
                    boundaryGap: false
                }
            ],
            yAxis: {
                type: 'value',
                boundaryGap: [0, '100%'],
                min(value) {
                    return value.min;
                },
                max(value) {
                    return value.max;
                },
                axisLabel: {
                    formatter(value, index) {
                        // keep 0.11111 to 0.111
                        return value.toString().slice(0, 5);
                    }
                }
            },
            series: seriesOption
        };
        this.myChart.setOption(option);
    },
    setChartsData({tagList, tag}) {
        let requestList = tagList.map(item => {
            let params = {
                run: item.run,
                tag: tag
            };
            return getPluginScalarsScalars(params);
        });
        axios.all(requestList).then(resArray => {
            let seriesData = resArray.map(res => {
                return {
                    data: res.data,
                    encode: {
                        // map 1 to xAixs。
                        x: [1],
                        // map 2 to yAixs。
                        y: [2]
                    }
                };
            });
            this.myChart.setOption({
                series: seriesData
            });
        });
    },
    getChartOptions() {
        return this.myChart.getOption() || {};
    },
    handleDownLoad() {
        let options = this.getChartOptions();
        let series = options.series || [];
        let downloadType = this.data.get('downloadType');
        let sery = series.find(item => item.name === downloadType) || {};
        let tagInfo = this.data.get('tagInfo');
        let fileName = tagInfo.tag.replace(/\//g, '-');
        generateJsonAndDownload(sery.data, fileName);
    }
};
</script>
<style lang="stylus">
    .visual-dl-charts
        float left
        .visual-dl-chart-actions
            .sm-form-item
                width 300px
                display inline-block
</style>