【问题标题】:Customize tooltip in dxChart在 dxChart 中自定义工具提示
【发布时间】:2016-06-24 06:37:21
【问题描述】:

我正在使用 DevExtreme 的 dxchart 迈出第一步。目前我有以下代码:

HTML:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <title>DevExtreme Chart</title>
        <!--FRAMEWOKR-->
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/2.2.3/jquery.min.js"></script>
        <script src="./lib/globalize.min.js"></script>
        <script src="./lib/dx.charts.js"></script>
        <!--JS-->
        <script type="text/javascript" src="chart.js"></script>
    </head>
    <body>
        <div id="chartContainer" style="width:100%; height: 440px"></div>
    </body>
</html>

JS:

$(document).ready(function(){
    var dataSource = [
        {
            argument: '15.06.2016',
            puls: 102,
            temperatur: 37.6,
            weight: 91
        },
        {
            argument: '16.06.2016',
            puls: 99,
            temperatur: 35.1,
            weight: 88
        },
        {
            argument: '17.06.2016',
            puls: 87,
            temperatur: 38.0,
            weight: 87
        },
        {
            argument: '18.06.2016',
            puls: 91,
            temperatur: 36.3,
            weight: 87
        },
        {
            argument: '19.06.2016',
            puls: 112,
            temperatur: 37.1,
            weight: 90
        }
    ];

    $("#chartContainer").dxChart({
        dataSource: dataSource,
        commonSeriesSettings: {
            type: "spline",
            label: {
                visible: false,
                connector: {
                    visible: false
                }
            },
            argumentField: "argument",
            axis: "pulsAxe"
        },
        tooltip: {
            enabled: true,
            customizeTooltip: function(obj) {
                return {
                    text: obj.seriesName
                }
            }
        },
        legend: {
            verticalAlignment: "top",
            horizontalAlignment: "right"
            },
        "export": {
            enabled: true
        },
        title: {
            text: "Hugo Amacher | 15.08.1977 (M)",
            subtitle: {
                enabled: true,
                text: "Ich bin ein Untertitel..."
            }
        },
        zoomingMode: "all",
        series: [
            {
                name: "Puls",
                valueField: "puls"
            },
            {
                name: "Temperatur",
                valueField: "temperatur",
                axis: "temperaturAxe"
            },
            {
                name: "Gewicht",
                valueField: "weight",
                axis: "weightAxe"
            }
        ],
        valueAxis: [
            {
                name: "pulsAxe",
                title: "Puls",
                position: "left",
                label: {
                    customizeText: function(value) {
                        return value.value + " bpm"
                    }
                }
            },
            {
                name: "temperaturAxe",
                title: "Temperatur",
                position: "left",
                label: {
                    customizeText: function(value) {
                        return value.value + " °C"
                    }
                }
            },
            {
                name: "weightAxe",
                title: "Gewicht",
                position: "left",
                label: {
                    customizeText: function(value) {
                        return value.value + " kg"
                    }
                }
            }
        ]
    });
});

我的结果是这样的:

如您所见,有一个简单的样条图,图表上有三个不同的 y 轴和三个不同的值范围。对于每把斧头,我都有一个标签(bpm、°C 和 kg)。我还实现了 dxchart 的工具提示。在我悬停一个点的那一刻,工具提示中只有值。我还想在值之后动态添加到工具提示的标签。这意味着,当我将 ax “Puls” 的值悬停时,它应该显示例如:91 bpm。我怎样才能做到这一点。我用customizeTooltip试了一下:http://js.devexpress.com/Documentation/ApiReference/Data_Visualization_Widgets/dxChart/Configuration/tooltip/?version=16_1#customizeTooltip

我有这样的想法,检查 seriesName 并在返回中分配标签,如下所示,但它不起作用:

tooltip: {
    enabled: true,
    customizeTooltip: function(point) {
        if (point.seriesName == "Puls") {
            return {
                text: point.value + " bpm"
            }
        } else if (point.seriesName == "Gewicht") {
            return {
                text: point.value + " kg"
            }                    
        } else if (point.seriesName == "Temperatur") {
            return {
                text: point.value + " °C"
            }                         
        }
    }
},

【问题讨论】:

    标签: javascript charts devexpress tooltip devextreme


    【解决方案1】:

    你好,请检查Plunkerhttps://plnkr.co/edit/6Uoh30bb8qBNWIIbyX0O?p=preview

    检查你的图书馆包括

    HTML

    <!DOCTYPE html>
    <html>
    
      <head>
        <meta charset="utf-8" />
        <title></title>
        <link rel="stylesheet" href="style.css" />
    <script type="text/javascript" src="https://ajax.aspnetcdn.com/ajax/jquery/jquery-2.1.4.min.js"></script>
    <script type="text/javascript" src="https://ajax.aspnetcdn.com/ajax/globalize/0.1.1/globalize.min.js"></script>
    <script type="text/javascript" src="https://cdn3.devexpress.com/jslib/15.2.5/js/dx.chartjs.js"></script>
        <script src="script.js"></script>
      </head>
    
      <body>
        <div id="chartContainer" style="width:100%; height: 440px"></div>
        <!-- Put your html here! -->
      </body>
    
    </html>
    

    JS

    // Add your javascript here
    $(function(){
      var dataSource = [
            {
                argument: '15.06.2016',
                puls: 102,
                temperatur: 37.6,
                weight: 91
            },
            {
                argument: '16.06.2016',
                puls: 99,
                temperatur: 35.1,
                weight: 88
            },
            {
                argument: '17.06.2016',
                puls: 87,
                temperatur: 38.0,
                weight: 87
            },
            {
                argument: '18.06.2016',
                puls: 91,
                temperatur: 36.3,
                weight: 87
            },
            {
                argument: '19.06.2016',
                puls: 112,
                temperatur: 37.1,
                weight: 90
            }
        ];
    
        $("#chartContainer").dxChart({
            dataSource: dataSource,
            commonSeriesSettings: {
                type: "spline",
                label: {
                    visible: false,
                    connector: {
                        visible: false
                    }
                },
                argumentField: "argument",
                axis: "pulsAxe"
            },
            tooltip: {
                enabled: true,
                customizeTooltip: function(obj) {
                  console.log(obj.seriesName)
                  if (obj.seriesName == "Puls") {
                return {
                    text: obj.value + " bpm"
                }
            } else if (obj.seriesName == "Gewicht") {
                return {
                    text: obj.value + " kg"
                }                    
            } else if (obj.seriesName == "Temperatur") {
                return {
                    text: obj.value + " °C"
                }                         
            }
    
                    // return {
                    //     text: obj.seriesName
                    // }
                }
            },
            legend: {
                verticalAlignment: "top",
                horizontalAlignment: "right"
                },
            "export": {
                enabled: true
            },
            title: {
                text: "Hugo Amacher | 15.08.1977 (M)",
                subtitle: {
                    enabled: true,
                    text: "Ich bin ein Untertitel..."
                }
            },
            zoomingMode: "all",
            series: [
                {
                    name: "Puls",
                    valueField: "puls"
                },
                {
                    name: "Temperatur",
                    valueField: "temperatur",
                    axis: "temperaturAxe"
                },
                {
                    name: "Gewicht",
                    valueField: "weight",
                    axis: "weightAxe"
                }
            ],
            valueAxis: [
                {
                    name: "pulsAxe",
                    title: "Puls",
                    position: "left",
                    label: {
                        customizeText: function(value) {
                            return value.value + " bpm"
                        }
                    }
                },
                {
                    name: "temperaturAxe",
                    title: "Temperatur",
                    position: "left",
                    label: {
                        customizeText: function(value) {
                            return value.value + " °C"
                        }
                    }
                },
                {
                    name: "weightAxe",
                    title: "Gewicht",
                    position: "left",
                    label: {
                        customizeText: function(value) {
                            return value.value + " kg"
                        }
                    }
                }
            ]
        });
    });
    

    【讨论】:

    • 您好,感谢您的回答,但这不起作用?!
    • 查看我附上的截图
    • 嗨,您的脚本包括我的屏幕是白色的,并且在控制台中出现跟随错误:GET cdn3.devexpress.com/jslib/15.2.5/js/dx.chartjs.js net::ERR_INSECURE_RESPONSE 干杯
    • 我在 plunker 中创建了 plunker 将允许 https 所以这不是问题
    • 如果您启用受信任的站点,即使您也可以使用 https .. 快乐编码@MrBuggy
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-23
    • 2011-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多