You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@echarts.apache.org by GitBox <gi...@apache.org> on 2022/12/16 07:08:11 UTC

[GitHub] [echarts] Ovilia commented on a diff in pull request #17579: Feat/box plot labels

Ovilia commented on code in PR #17579:
URL: https://github.com/apache/echarts/pull/17579#discussion_r1050426707


##########
test/boxplot-labels.html:
##########
@@ -0,0 +1,256 @@
+<!DOCTYPE html>
+<!--
+Licensed to the Apache Software Foundation (ASF) under one
+or more contributor license agreements.  See the NOTICE file
+distributed with this work for additional information
+regarding copyright ownership.  The ASF licenses this file
+to you under the Apache License, Version 2.0 (the
+"License"); you may not use this file except in compliance
+with the License.  You may obtain a copy of the License at
+
+   http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing,
+software distributed under the License is distributed on an
+"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+KIND, either express or implied.  See the License for the
+specific language governing permissions and limitations
+under the License.
+-->
+
+
+<html>
+    <head>
+        <meta charset="utf-8">
+        <meta name="viewport" content="width=device-width, initial-scale=1" />
+        <script src="lib/simpleRequire.js"></script>
+        <script src="lib/config.js"></script>
+        <script src="lib/jquery.min.js"></script>
+        <script src="lib/facePrint.js"></script>
+        <script src="lib/testHelper.js"></script>
+        <!-- <script src="ut/lib/canteen.js"></script> -->
+        <link rel="stylesheet" href="lib/reset.css" />
+    </head>
+    <body>
+        <style>
+        </style>
+        <div id="main"></div>
+        <script>
+         /**
+             * @see <https://en.wikipedia.org/wiki/Michelson%E2%80%93Morley_experiment>
+             * @see <http://bl.ocks.org/mbostock/4061502>
+             */
+             var chart;
+            var data;
+            var mean;
+
+            require([
+                'echarts',
+                'data/Michelson-Morley.json.js'
+            ], function (echarts, rawData) {
+                var env = echarts.env;
+
+                chart = echarts.init(document.getElementById('main'));
+
+                update('horizontal');
+
+                initControlPanel(env);
+
+                function update(layout) {
+
+                    // mean = calculateMean(rawData);
+
+                    var categoryAxis = {
+                        type: 'category',
+                        boundaryGap: true,
+                        nameGap: 30,
+                        splitArea: {
+                            show: false
+                        },
+                        splitLine: {
+                            show: false
+                        }
+                    };
+                    var valueAxis = {
+                        type: 'value',
+                        name: 'km/s minus 299,000',
+                        splitArea: {
+                            show: true
+                        }
+                    };
+
+                    chart.setOption({
+                        aria: {
+                            show: true
+                        },
+                        dataset: [{
+                            source: rawData
+                        }, {
+                            transform: {
+                                type: 'boxplot',
+                                config: { itemNameFormatter: 'expr {value}' }
+                            }
+                        }, {
+                            fromDatasetIndex: 1,
+                            fromTransformResult: 1
+                        }],
+                        title: [
+                            {
+                                text: 'Michelson-Morley Experiment',
+                                left: 'center'
+                            },
+                            {
+                                text: 'upper: Q3 + 1.5 * IRQ \nlower: Q1 - 1.5 * IRQ',
+                                borderColor: '#999',
+                                borderWidth: 1,
+                                textStyle: {
+                                    fontSize: 14
+                                },
+                                left: '10%',
+                                top: '90%'
+                            }
+                        ],
+                        legend: {
+                            left: 'right'
+                        },
+                        tooltip: {
+                            trigger: 'item',
+                            axisPointer: {
+                                type: 'shadow'
+                            }
+                        },
+                        grid: {
+                            left: '10%',
+                            right: '10%',
+                            bottom: '15%'
+                        },
+                        xAxis: layout === 'horizontal' ? categoryAxis : valueAxis,
+                        yAxis: layout === 'vertical' ? categoryAxis : valueAxis,
+                        series: [
+                            {
+                                name: 'boxplot',
+                                type: 'boxplot',
+                                datasetIndex: 1,
+
+                                markPoint: {
+                                    data: [
+                                        {
+                                            name: '某个坐标',
+                                            coord: [2, 300]
+                                        },
+                                        {
+                                            name: '某个屏幕坐标',
+                                            x: 100,
+                                            y: 200,
+                                            label: {
+                                                normal: {
+                                                    show: false,
+                                                    formatter: 'asdf'
+                                                },
+                                                emphasis: {
+                                                    show: true,
+                                                    position: 'top',
+                                                    formatter: 'zxcv'
+                                                }
+                                            }
+                                        },
+                                        {
+                                            name: 'max value (default)',
+                                            type: 'max'
+                                        },
+                                        {
+                                            name: 'min value (default)',
+                                            type: 'min'
+                                        },
+                                        {
+                                            name: 'max value (dim:Q1)',
+                                            type: 'max',
+                                            valueDim: 'Q1'
+                                        },
+                                        {
+                                            name: 'average value (dim:Q1)',
+                                            type: 'average',
+                                            valueDim: 'Q1'
+                                        }
+                                    ]
+                                },
+
+                                markLine: {
+                                    data: [
+                                        [
+                                            {name: '两个坐标之间的标线', coord: [1, 240]},
+                                            {coord: [2, 260]}
+                                        ],
+                                        [
+                                            {name: '两个屏幕坐标之间的标线', x: 50, y: 60},
+                                            {x: 70, y: 90}
+                                        ],
+                                        [
+                                            {name: 'max - min', type: 'max'},
+                                            {type: 'min'}
+                                        ],
+                                        {
+                                            name: 'min line',
+                                            type: 'min'
+                                        },
+                                        {
+                                            name: 'max line on dim:Q3',
+                                            type: 'max',
+                                            valueDim: 'Q3'
+                                        }
+                                    ]
+                                },
+                                label: {
+                                    show: true,
+                                    formatter: function (param) {

Review Comment:
   If `formatter` is not provided here, there will be an error. You should not assert it to be defined.



##########
src/chart/boxplot/BoxplotView.ts:
##########
@@ -180,11 +184,19 @@ function updateNormalBoxData(
 
     el.useStyle(data.getItemVisual(dataIndex, 'style'));
     el.style.strokeNoScale = true;
-
     el.z2 = 100;
 
     const itemModel = data.getItemModel<BoxplotDataItemOption>(dataIndex);
     const emphasisModel = itemModel.getModel('emphasis');
+    const labelModel = seriesModel.get('label');
+
+    if (labelModel.show) {
+        const formattedLabels =
+            ((seriesModel as BoxplotSeriesModel).getRawValue(dataIndex) as number[])
+            .splice(1).map((value: number) => labelModel.formatter(value).toString());

Review Comment:
   This is not the correct way to format labels. If `formatter` is not defined, this should go wrong. You should probably do like [sunburst](https://github.com/apache/echarts/blob/master/src/chart/sunburst/SunburstPiece.ts#L189).



##########
src/label/labelStyle.ts:
##########
@@ -292,6 +293,66 @@ function setLabelStyle<TLabelDataIndex>(
 }
 export { setLabelStyle };
 
+/**
+ * Set and create specific labels for box plot.
+ *
+ * This method should only be used for this case,
+ * as it manually creates multiple alternating labels
+ * specifically for the box plot.
+ */
+export function setBoxLabels(boxItemPositions: number[][], formattedLabels: Array<string>,
+    labelOption: any, group: ViewRootGroup) {
+    // get sorted and unique y positions of box items
+    const yPositions = boxItemPositions.map((pos: number[]) => pos[1]);
+    const uniqueYPositions: number[] = [];
+    yPositions.forEach(position => {
+        if (!uniqueYPositions.includes(position)) {
+            uniqueYPositions.push(position);
+        }
+    });
+    uniqueYPositions.sort(function (a: number, b: number) {
+        return b - a;
+    });
+
+    boxItemPositions.sort(function (a: number[], b: number[]) {
+        return a[0] - b[0];
+    });
+
+    // get alternating y positions for labels to avoid overlap
+    const uniqueAlternatingPositions = uniqueYPositions.map((posY: number, ind: number) => {
+            const matchingPositions = boxItemPositions.filter((orgPos: number[]) => orgPos[1] === posY);
+            const index = (ind % 2 === 0) ? 0 : matchingPositions.length - 1;
+            return matchingPositions[index];
+    });
+
+    // create labels and add them to their respective positions
+    formattedLabels.forEach((labelText: string, ind: number) => {
+        if (labelOption.show === 'iqr' && (ind === 0 || ind === 4)) {
+            return;
+        }
+        if (labelOption.show === 'median' && (ind !== 2)) {
+            return;
+        }
+        if (labelOption.show === 'whiskers' && (ind !== 0 && ind !== 4)) {
+            return;
+        }
+        const label = new graphic.Text({
+            style: {
+                text: labelText
+            },
+            z2: 1000
+        });
+        const defaultOffset = 5;
+        const defaultXOffset = (ind % 2) === 0 ? (-(label.getBoundingRect().width + defaultOffset)) : defaultOffset;

Review Comment:
   As I said before, we should provide a way to let developers decide where to display the label to the left or right. The `label.align` can be changed into `'left' | 'right' | 'center' | { iqr: 'left' | 'right' | 'center' , ... }`.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@echarts.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@echarts.apache.org
For additional commands, e-mail: commits-help@echarts.apache.org