parallel-nutrients.html 8.8 KB
Newer Older
P
pah100 已提交
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 207 208 209 210 211 212 213 214 215 216 217 218 219
<!DOCTYPE>
<html>
    <head>
        <meta charset="utf-8">
        <script src="esl.js"></script>
        <script src="config.js"></script>
        <script src="lib/jquery.min.js"></script>
        <meta name="viewport" content="width=device-width, initial-scale=1" />
        <link rel="stylesheet" href="reset.css">
    </head>
    <body>
        <style>
            body {
                background: #333;
            }
            #main {
                height: 400px;
            }
        </style>
        <div id="main"></div>
        <script>

            var echarts;
            var colorTool;
            var chart;
            var myChart;
            var groupCategories = [];
            var groupColors = [];

            require([
                'echarts',
                'zrender/tool/color',
                'echarts/chart/parallel',
                'echarts/component/legend',
                'echarts/component/tooltip',
                'echarts/component/toolbox',
                'echarts/component/visualMap',
            ], function (ec, ct) {

                $.getJSON('./data/nutrients.json', function (data) {
                    echarts = ec;
                    colorTool = ct;

                    normalizeData(data);

                    chart = myChart = echarts.init(document.getElementById('main'));

                    chart.setOption(getOption(data));
                });

                var indices = {
                    name: 0,
                    group: 1,
                    id: 16
                };
                var schema = [
                    {name: 'name', index: 0},
                    {name: 'group', index: 1},
                    {name: 'protein', index: 2},
                    {name: 'calcium', index: 3},
                    {name: 'sodium', index: 4},
                    {name: 'fiber', index: 5},
                    {name: 'vitaminc', index: 6},
                    {name: 'potassium', index: 7},
                    {name: 'carbohydrate', index: 8},
                    {name: 'sugars', index: 9},
                    {name: 'fat', index: 10},
                    {name: 'water', index: 11},
                    {name: 'calories', index: 12},
                    {name: 'saturated', index: 13},
                    {name: 'monounsat', index: 14},
                    {name: 'polyunsat', index: 15},
                    {name: 'id', index: 16}
                ];

                function normalizeData(originData) {
                    var groupMap = {};
                    originData.forEach(row => {
                        var groupName = row[indices.group];
                        if (!groupMap.hasOwnProperty(groupName)) {
                            groupMap[groupName] = 1;
                        }
                    });

                    originData.forEach(row => {
                        row.forEach((item, index) => {
                            if (index !== indices.name
                                && index !== indices.group
                                && index !== indices.id
                            ) {
                                // Convert null to zero, as all of them under unit "g".
                                row[index] = parseFloat(item) || 0;
                            }
                        });
                    });

                    for (var groupName in groupMap) {
                        if (groupMap.hasOwnProperty(groupName)) {
                            groupCategories.push(groupName);
                        }
                    }
                    var hStep = Math.round(300 / (groupCategories.length - 1));
                    for (var i = 0; i < groupCategories.length; i++) {
                        groupColors.push(colorTool.modifyHSL('#5A94DF', hStep * i));
                    }
                }

                function getOption(data) {

                    var lineStyle = {
                        normal: {
                            width: 1,
                            opacity: 0.01
                            // shadowBlur: 10,
                            // shadowOffsetX: 0,
                            // shadowOffsetY: 0,
                            // shadowColor: 'rgba(0, 0, 0, 0.5)'
                        }
                    };

                    return {
                        backgroundColor: '#333',
                        tooltip: {
                            padding: 10,
                            backgroundColor: '#222',
                            borderColor: '#777',
                            borderWidth: 1,
                            formatter: function (obj) {
                                var value = obj[0].value;
                                return '<div style="border-bottom: 1px solid rgba(255,255,255,.3); font-size: 18px;padding-bottom: 7px;margin-bottom: 7px">'
                                    + schema[1].name + '' + value[1] + '<br>'
                                    + schema[2].name + '' + value[2] + '<br>'
                                    + schema[3].name + '' + value[3] + '<br>'
                                    + schema[4].name + '' + value[4] + '<br>'
                                    + schema[5].name + '' + value[5] + '<br>'
                                    + schema[6].name + '' + value[6] + '<br>';
                            }
                        },
                        visualMap: {
                            show: true,
                            type: 'piecewise',
                            categories: groupCategories,
                            dimension: indices.group,
                            inRange: {
                                color: groupColors //['#d94e5d','#eac736','#50a3ba']
                            },
                            outOfRange: {
                                color: groupColors //['#d94e5d','#eac736','#50a3ba']
                            }
                        },
                        parallelAxis: [
                            {dim: 2, name: schema[2].name},
                            {dim: 4, name: schema[4].name},
                            {dim: 3, name: schema[3].name},
                            {dim: 5, name: schema[5].name},
                            {dim: 6, name: schema[6].name},
                            {dim: 7, name: schema[7].name},
                            {dim: 8, name: schema[8].name},
                            {dim: 9, name: schema[9].name},
                            {dim: 10, name: schema[10].name},
                            {dim: 11, name: schema[11].name},
                            {dim: 12, name: schema[12].name},
                            {dim: 13, name: schema[13].name},
                            {dim: 14, name: schema[14].name},
                            {dim: 15, name: schema[15].name},
                            {dim: 16, name: schema[16].name, scale: true}
                        ],
                        parallel: {
                            bottom: 100,
                            parallelAxisDefault: {
                                type: 'value',
                                name: 'nutrients',
                                nameLocation: 'end',
                                nameGap: 20,
                                nameTextStyle: {
                                    color: '#fff',
                                    fontSize: 14
                                },
                                axisLine: {
                                    lineStyle: {
                                        color: '#aaa'
                                    }
                                },
                                axisTick: {
                                    lineStyle: {
                                        color: '#777'
                                    }
                                },
                                splitLine: {
                                    show: false
                                },
                                axisLabel: {
                                    textStyle: {
                                        color: '#fff'
                                    }
                                }
                            }
                        },
                        animation: false,
                        series: [
                            {
                                name: 'nutrients',
                                type: 'parallel',
                                lineStyle: lineStyle,
                                inactiveOpacity: 0,
                                activeOpacity: 0.01,
                                progressive: 100,
                                smooth: true,
                                data: data
                            }
                        ]
                    };
                }

            });

        </script>
    </body>
</html>