Highcharts饼图可以有url链接

Highcharts pie charts can have url links

本文关键字:url 链接 Highcharts      更新时间:2023-09-26

我在应用程序中使用Highcharts饼图,其中饼图的数据是从数据库填充的。我的问题是在填充饼状图后,如果我点击某些区域,它应该渲染到特定的php页面。这可能吗?

下面是我的代码:
    function open_risk_level_pie()
    {
    $chart = new Highchart();
    $chart->chart->renderTo = "open_risk_level_pie";
    $chart->chart->plotBackgroundColor = null;
    $chart->chart->plotBorderWidth = null;
    $chart->chart->plotShadow = false;
    $chart->title->text = "Risk Level";
    $chart->tooltip->formatter = new HighchartJsExpr("function() {
    return '<b>'+ this.point.name +'</b>: '+ this.point.y; }");
    $chart->plotOptions->pie->allowPointSelect = 1;
    $chart->plotOptions->pie->cursor = "pointer";
    $chart->plotOptions->pie->dataLabels->enabled = false;
    $chart->plotOptions->pie->showInLegend = 1;
    $chart->plotOptions->pie->colors = array('red', 'orange', 'yellow', 'black');
    $chart->credits->enabled = false;
    // Open the database connection
    $db = db_open();
// Get the risk levels
$stmt = $db->prepare("SELECT * from `risk_levels`");
$stmt->execute();
$array = $stmt->fetchAll();
$high = $array[0][0];
$medium = $array[1][0];
$low = $array[2][0];
    // Query the database
    $stmt = $db->prepare("select a.calculated_risk, COUNT(*) AS num, CASE WHEN a.calculated_risk >= " . $high . " THEN 'High' WHEN a.calculated_risk < " . $high . " AND a.calculated_risk >= " . $medium . " THEN 'Medium' WHEN a.calculated_risk < " . $medium . " AND a.calculated_risk >= " . $low . " THEN 'Low' WHEN a.calculated_risk < " . $low . " AND a.calculated_risk >= 0 THEN 'Insignificant' END AS level from `risk_scoring` a JOIN `risks` b ON a.id = b.id WHERE b.status != '"Closed'" GROUP BY level ORDER BY a.calculated_risk DESC");
    $stmt->execute();
    // Store the list in the array
    $array = $stmt->fetchAll();
    // Close the database connection
    db_close($db);
    // If the array is empty
    if (empty($array))
    {
            $data[] = array("No Data Available", 0);
    }
    // Otherwise
    else
    {
            // Create the data array
            foreach ($array as $row)
            {
                    $data[] = array($row['level'], (int)$row['num']);
            }
            $chart->series[] = array('type' => "pie",
                    'name' => "Level",
                    'data' => $data);
    }
echo "<div id='"open_risk_level_pie'"></div>'n";
echo "<script type='"text/javascript'">";
echo $chart->render("open_risk_level_pie");
echo "</script>'n";

}

highcharts网站上有一个类似的例子。你可以使用point/events/click函数来触发一个新的页面加载:

 plotOptions: {
        series: {
            cursor: 'pointer',
            point: {
                events: {
                    click: function() {
                        location.href = this.options.url;
                    }
                }
            }
        }
    },
http://jsfiddle.net/w5Lx4/

注意,在本例中,URL被添加到系列数据中:

 data: [{
            y: 29.9,
            url: 'http://bing.com/search?q=foo'
        }, {

您可以将其替换为基于点y值生成URL。