Highcharts饼图可以有url链接

时间:2014-06-28 09:59:39

标签: javascript php jquery charts highcharts

我在我的应用程序中使用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";

}

1 个答案:

答案 0 :(得分:5)

在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来替换它。