如何将图表输出导出为JPEG或png格式?

时间:2019-07-03 01:43:11

标签: javascript asp.net-mvc highcharts

我已经在项目中创建了输出图表。图表运行良好。但我不知道该如何以png或JPEG格式导出此图表。

许多从Internet导出源代码的过程都是使用其自己的图表。就我而言,我使用了Highcharts。谁能帮我吗?

这是我的数据库代码

using (SqlConnection sqlcon = new SqlConnection(connstring))
        {
            //canteen 1
            List<SurveyQtnResult> resQ1 = new List<SurveyQtnResult>();
            SqlCommand cmd = new SqlCommand("select ans,isnull(count(ans),0) as cAns from DB_CNTS.dbo.SurveyResultBD_tbl where canteenNo = "+ cNum + " and qNum = 1 and month(dateCreated) = " + month + " and year(dateCreated) = " + year + " group by ans", sqlcon);
            cmd.CommandType = CommandType.Text;
            cmd.CommandTimeout = 1000000;
            sqlcon.Open();  
            SqlDataReader rdr = cmd.ExecuteReader();

            while (rdr.Read())
            {
                SurveyQtnResult q1 = new SurveyQtnResult();
                q1.name = rdr["ans"].ToString();
                q1.y = Convert.ToInt32(rdr["cAns"]);
                resQ1.Add(q1);
            }
            ViewBag.resQ1 = resQ1.ToList();
            ViewBag.DataPoints = JsonConvert.SerializeObject(resQ1.ToList(), _jsonSetting);
        }

这是我的观点

<div class="box col-lg-4">
     <div id="container" style="height:500px; width:500px;"></div>
</div>


<script type="text/javascript">
// Radialize the colors
            Highcharts.setOptions({
                colors: Highcharts.map(Highcharts.getOptions().colors, function (color) {
                    return {
                        radialGradient: {
                            cx: 0.5,
                            cy: 0.3,
                            r: 0.7
                        },
                        stops: [
                            [0, color],
                            [1, Highcharts.Color(color).brighten(-0.3).get('rgb')] // darken
                        ]
                    };
                })
            });

            // Build the chart Q 1
            Highcharts.chart('container', {
                chart: {
                    plotBackgroundColor: null,
                    plotBorderWidth: null,
                    plotShadow: false,
                    type: 'pie'
                },
                title: {
                    text: 'Question 1 Results'
                },
                plotOptions: {
                    pie: {
                        allowPointSelect: true,
                        cursor: 'pointer',
                        dataLabels: {
                            enabled: true,
                            format: '<b>{point.name}</b>: {point.y} ',
                            style: {
                                color: (Highcharts.theme && Highcharts.theme.contrastTextColor) || 'black'
                            },
                            connectorColor: 'silver'
                        }
                    }
                },
                series: [{
                    name: 'VOTES',
                    data: @Html.Raw(ViewBag.DataPoints)
                    }]
            });


</script>

有人对我有什么想法吗,我怎么能达到期望的输出?

1 个答案:

答案 0 :(得分:0)

您可以获得图表instance并使用exportChart函数。

cshtml

<button id="btnPng">Export to PNG</button>

脚本

var chart =  Highcharts.chart('container', {}) // your code

$("#btnPng").on('click', function(event) {
    chart.exportChart({type: "image/png"});
});