0

once again I apologise for my naivety. I would now like to add a suffix to my tooltip to indicate % humidity and °C for temperature. the result I derived from ppotaczek (thanks) with a few small modifications is good, but when I try to add a valueSuffix in a variety of ways, it always comes back undefined, probably due to the multiple series (I will be adding more series later that will include the symbol for pressure). I have tried adding a variable to the series and using valueSuffix but I can't get it right. I am happy with the rest of the code but would always appreciate advice.

Tooltip as it is - suffix undefined

I hope I have been clear. Many thanks in advance

...

<?php $json_data = include ('database.php');
?> 
<!DOCTYPE HTML>
<html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Line Graph</title>

    </head>
    <body>
<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/modules/exporting.js"></script>
<script src="https://code.highcharts.com/modules/export-data.js"></script>
<script src="https://code.highcharts.com/modules/accessibility.js"></script>
<script src="https://code.highcharts.com/modules/series-label.js"></script>


        <style type="text/css">
        #container {
            min-width: 310px;
            max-width: 800px;
            height:400px;
            margin: 0 auto
        }
        </style>

<div id="container"></div>


<script type="text/javascript">

var data = <?= $json_data?>

var series = [{
        name: "Temperature",
        valueSuffix: '°C',
        data: []

    },
    {
        name: "Humidity",
        valueSuffix: '%',
        data: [],
        yAxis: 1
    }
];
//alert(series[1].appendor);// from a previous attempt

data.forEach(function(dataEl) {
    series[0].data.push([
        new Date(dataEl.Timestamp).getTime(),
        Number(dataEl.temperature)
    ]);
    series[1].data.push([
        new Date(dataEl.Timestamp).getTime(),
        Number(dataEl.humidity)
    ]);
});

Highcharts.chart('container', {
     
    series: series,
    yAxis: [{
            title: {
                text: 'Temperature'
                
            }
            },
        {
            title: {
                text: 'Humidity'
            },
            opposite: true
        }
    ],
    title: {
      text: 'Temperature and Humidity',
      x: -20 //center
    },
    subtitle: {
      text: 'Source: The Caravan',
      x: -20
    },   
 tooltip: {
        formatter: function () {
            var suff = this.series.valueSuffix, heading = this.series.name
            return "<b>" + heading + "</b><br/>" + Highcharts.dateFormat('%a %d %b %H:%M.', this.x, true) + ":<br/>" + Highcharts.numberFormat(this.y, 0) + suff +" " ;
        },
        backgroundColor: 'rgba(0, 0, 0, .75)',
        borderWidth: 2,
        style: {
            color: '#CCCCCC'
        }
    },

 // fix for x-axxis from: https://stackoverflow.com/questions/7101464/how-to-get-highcharts-dates-in-the-x-axis 
   xAxis: {
    type: 'datetime',
    labels: {
      formatter: function() {
        return Highcharts.dateFormat('%a %d %b', this.value);
      }
    }
  },
 
});

</script>

</body>

</html>

...

Here is a block of the json_data that is being read from a Mysql database - which is dynamic and changing all of the time: [{"Timestamp":"10:04 02/01/21","temperature":"5","humidity":"66"},{"Timestamp":"10:19 02/01/21","temperature":"6","humidity":"65"},{"Timestamp":"10:35 02/01/21","temperature":"6","humidity":"64"},{"Timestamp":"10:50 02/01/21","temperature":"6","humidity":"64"},{"Timestamp":"11:06 02/01/21","temperature":"6","humidity":"64"},{"Timestamp":"11:21 02/01/21","temperature":"7","humidity":"63"},{"Timestamp":"11:34 02/01/21","temperature":"10","humidity":"66"},{"Timestamp":"04:21 02/01/21","temperature":"15","humidity":"64"},{"Timestamp":"04:36 02/01/21","temperature":"16","humidity":"61"},{"Timestamp":"04:51 02/01/21","temperature":"15","humidity":"59"},{"Timestamp":"05:07 02/01/21","temperature":"15","humidity":"60"},{"Timestamp":"05:22 02/01/21","temperature":"14","humidity":"61"}]

1 Answers1

0

There is no valueSuffix property stored directly in a series because it is a custom one and it is saved in series.options.

    tooltip: {
        formatter: function() {
            var suff = this.series.options.valueSuffix,
                heading = this.series.name;

            return "<b>" + heading + "</b><br/>" + Highcharts.dateFormat('%a %d %b %H:%M.', this.x, true) + ":<br/>" + Highcharts.numberFormat(this.y, 0) + suff + " ";
        },
        ...
    }

Live demo: http://jsfiddle.net/BlackLabel/1se5cyd7/

ppotaczek
  • 36,341
  • 2
  • 14
  • 24
  • Thanks again "Black Label" (is that your favourite whisky?). I got pretty close to a similar solution, using 'suff = this.series.valueSuffix" which didn't work because I didn’t really understand the methods. I tried var series = [{ name: "Temperature", data: [], tooltip: { valueSuffix: '°C', } }, Which added the suffix but lost (Heading, date; formatted the way I wanted it etc.) I’m gradually learning but there is obviously a long way to go. Now I want to achieve the same thing with Google Charts, Cheers - Huggie Duggie – HuggieDuggie Mar 10 '21 at 20:11
  • Can you tell me what this does please? series[0].data.sort(function(a, b) { return a[0] - b[0]; }); series[1].data.sort(function(a, b) { return a[0] - b[0]; }); Thanks, Huggies – HuggieDuggie Mar 10 '21 at 20:16
  • Hi @HuggieDuggie, No, it is a company name: https://www.blacklabel.pl/ I recommend you to always check which values are available through logging `this` to a console. As to the question about sorting - your data was unsorted, so I just sorted it by x value :) Result without sorting: http://jsfiddle.net/BlackLabel/xdnps9fg/ – ppotaczek Mar 11 '21 at 08:32