在dxChart中自定义工具提示

时间:2016-06-24 06:37:22

标签: javascript charts devexpress tooltip devextreme

我正在使用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"
                    }
                }
            }
        ]
    });
});

我的结果是:

enter image description here

如您所见,有一个简单的样条图表,图表上的这个轴有三个不同的y轴和三个不同的值范围。对于每个斧头,我都有一个标签(bpm,°C和kg)。我还实现了dxchart的工具提示。当我悬停一个点时,在工具提示中只有值。我还想动态地在工具提示后添加值后面的标签。这意味着,当我将斧头“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"
            }                         
        }
    }
},

1 个答案:

答案 0 :(得分:4)

enter image description here您好请查看Plunker https://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"
                    }
                }
            }
        ]
    });
});