简体   繁体   English

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

[英]How can I export my chart output to JPEG or png format?

I already created an output chart in my project. 我已经在项目中创建了输出图表。 the chart is working well. 图表运行良好。 but I have no idea on, how can I export this chart in png or JPEG format. 但我不知道该如何以png或JPEG格式导出此图表。

Lots of exporting source code from the internet is using their own charts. 许多从Internet导出的源代码都使用自己的图表。 In my case I used Highcharts. 就我而言,我使用了Highcharts。 Can anyone help me? 谁能帮我?

This is my database code 这是我的数据库代码

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);
        }

this is my view 这是我的看法

<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>

is there anyone who have any idea on, how can I achieve my desired output? 有谁有什么想法,我怎么能达到期望的输出?

You can get chart instance and use exportChart function. 您可以获取图表instance并使用exportChart函数。

cshtml cshtml

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

script 脚本

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM