Custom Tooltips using Data Points

The above example shows you how to create LineChart using ChartJs package. In this example, for purpose of chart demonstration only, we do use mock-up data from array. As you can see, the KoolReport's widget in general support dataSource could be DataStore, Process, DataSource or even simple array.

This example shows how to build a line chart with modified tooltip interface using the custom property.

For example:

...
"tooltips" => array(
    ...
    "custom" => " function(tooltip) {
        ...
    }
)
...
<?php
require_once "../../../load.koolreport.php";
require_once "MyReport.php";
$report = new MyReport;
$report->run();
$report->render();
?>

<html>

<head>
    <title>
        Custom Tooltips using Data Points
    </title>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
    <style>
        canvas {
            -moz-user-select: none;
            -webkit-user-select: none;
            -ms-user-select: none;
        }

        .chartjs-tooltip {
            opacity: 1;
            position: absolute;
            background: rgba(0, 0, 0, .7);
            color: white;
            border-radius: 3px;
            -webkit-transition: all .1s ease;
            transition: all .1s ease;
            pointer-events: none;
            -webkit-transform: translate(-50%, 0);
            transform: translate(-50%, 0);
            padding: 4px;
        }

        .chartjs-tooltip-key {
            display: inline-block;
            width: 10px;
            height: 10px;
        }
    </style>
</head>

<body>
</body>

</html>
<?php
class MyReport extends \koolreport\KoolReport
{
    
}
<div id="canvas-holder1">
    <?php
    function randomScalingFactor()
    {
        return mt_rand(-100, 100);
    }
    $data = [
        ['month' => 'January', 'My First dataset' => randomScalingFactor(), 'My Second dataset' => randomScalingFactor()],
        ['month' => 'February', 'My First dataset' => randomScalingFactor(), 'My Second dataset' => randomScalingFactor()],
        ['month' => 'March', 'My First dataset' => randomScalingFactor(), 'My Second dataset' => randomScalingFactor()],
        ['month' => 'April', 'My First dataset' => randomScalingFactor(), 'My Second dataset' => randomScalingFactor()],
        ['month' => 'May', 'My First dataset' => randomScalingFactor(), 'My Second dataset' => randomScalingFactor()],
        ['month' => 'June', 'My First dataset' => randomScalingFactor(), 'My Second dataset' => randomScalingFactor()],
        ['month' => 'July', 'My First dataset' => randomScalingFactor(), 'My Second dataset' => randomScalingFactor()],
    ];

    \koolreport\chartjs\LineChart::create(array(
        'dataSource' => $data,
        'columns' => array(
            "month",
            "My First dataset" => array(
                "backgroundColor" => 'rgba(255, 99, 132, 0.2)',
                "borderColor" => 'rgb(255, 99, 132)',
                "pointBackgroundColor" => 'rgb(255, 99, 132)',
                "fill" => true
            ),
            "My Second dataset" => array(
                "backgroundColor" => 'rgba(54, 162, 235, 0.2) ',
                "borderColor" => 'rgb(54, 162, 235) ',
                "pointBackgroundColor" => 'rgb(54, 162, 235)',
                "fill" => true
            )
        ),
        "options" => array(
            "title" => array(
                "display" => true,
                "text" => 'Chart.js - Custom Tooltips using Data Points'
            ),
            "tooltips" => array(
                "mode" => 'index',
                "intersect" => false,
                "enabled" => false,
                "custom" => "function(tooltip) {
                    $(this._chart.canvas).css('cursor', 'pointer');

                    var positionY = this._chart.canvas.offsetTop;
                    var positionX = this._chart.canvas.offsetLeft;

                    $('.chartjs-tooltip').css({
                        opacity: 0,
                    });

                    if (!tooltip || !tooltip.opacity) {
                        return;
                    }

                    if (tooltip.dataPoints.length > 0) {
                        tooltip.dataPoints.forEach(function(dataPoint) {
                            var content = [dataPoint.xLabel, dataPoint.yLabel].join(': ');
                            var \$tooltip = $('#tooltip-' + dataPoint.datasetIndex);

                            \$tooltip.html(content);
                            \$tooltip.css({
                                opacity: 1,
                                top: positionY + dataPoint.y + 'px',
                                left: positionX + dataPoint.x + 'px',
                            });
                        });
                    }
                }",
            )
        )
    ));
    ?>
    <div class="chartjs-tooltip" id="tooltip-0"></div>
    <div class="chartjs-tooltip" id="tooltip-1"></div>
</div>

What People Are Saying

"KoolReport helps me very much in creating data report for my corporate! Keep up your good work!"
-- Alain Melsens

"The first use of your product. I was impressed by its easiness and powerfulness. This product is a great and amazing."
-- Dr. Lew Choy Onn

"Fantastic framework for reporting!"
-- Greg Schneider

Download KoolReport Get KoolReport Pro