Customize tooltip in dxChart

2019-07-28 15:18发布

问题:

I'm doing my first steps with dxchart of DevExtreme. At the moment I've got follow code:

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"
                    }
                }
            }
        ]
    });
});

My result is this:

As you can see, there is a simple spline chart, with three various y-axes and three various value-ranges for this axes on the chart. For each axe, I've got an label (bpm, °C and kg). I implemented also the tooltip of dxchart. At the moment when I hover a point, in the tooltip there is only the value. I also would like to add dynamicly to the tooltip the label after the value. That means, when I hover a value of the axe "Puls", it should show for example this: 91 bpm. How can I do this. I tried it with the customizeTooltip: http://js.devexpress.com/Documentation/ApiReference/Data_Visualization_Widgets/dxChart/Configuration/tooltip/?version=16_1#customizeTooltip

I had the idea, to check the seriesName and assign the label in the return something like this, but it didn't work:

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:

Hi please check the Plunker https://plnkr.co/edit/6Uoh30bb8qBNWIIbyX0O?p=preview

check your library includes

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>

and 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"
                    }
                }
            }
        ]
    });
});