解决使用Highcharts图表的.net导出服务,导出的图片右上角有个方块

场景:使用Highcharts图表导出图片,导出的图片右上角上有一个方块。

原因:对比php导出服务后,没有发现有方块,所以确定不是图表的问题,应该是导出服务的问题。

分析:通过查看Tek4.Highcharts.Exporting服务源码,发现原理是把图表生成svg后,然后解析svg,最后生成图片。所以有问题的地方只有svg的问题。

通过进一步的对生成的svg分析,终于得出了一个确切的结论,那就是svg的属性有问题(标红的地方),对于生成图片是多余的

<g style="padding-bottom: 0px; padding-left: 0px; padding-right: 0px; white-space: nowrap; cursor: default; padding-top: 0px" class="highcharts-tooltip" visibility="hidden"><rect fill="none" width="16" height="32" x="0.5" y="0.5" rx="3" ry="3" fill-opacity="0.85" stroke="black" stroke-opacity="0.05" stroke-width="5" transform="translate(1, 1)"></rect>
......
</g> 

Tek4.Highcharts.Exporting.ExportChart.cs


 internal static void ProcessExportRequest(HttpContext context)
    {
      if (context != null &&
        context.Request != null &&
        context.Response != null &&
        context.Request.HttpMethod == "POST")
      {
        HttpRequest request = context.Request;

        // Get HTTP POST form variables, ensuring they are not null.
        string filename = request.Form["filename"];
        string type = request.Form["type"];
        int width = 0;
        string svg = request.Form["svg"];
          //规避svg中的tooltip属性
        XmlDocument doc = new XmlDocument();
        doc.LoadXml(svg);
        XmlNodeList xnl = doc.GetElementsByTagName("g");

        for (int i = 0; i < xnl.Count; i++)
        {
            XmlNode xn = xnl[i];
            XmlElement xe = (XmlElement)xn;
            if (xe.GetAttribute("class") == "highcharts-tooltip")
            {
                xn.ParentNode.RemoveChild(xn);
                break;
            }
        }
        svg = doc.OuterXml;
        if (filename != null &&
          type != null &&
          Int32.TryParse(request.Form["width"], out width) &&
          svg != null)
        {
          // Create a new chart export object using form variables.
          Exporter export = new Exporter(filename, type, width, svg);

          // Write the exported chart to the HTTP Response object.
          export.WriteToHttpResponse(context.Response);

          // Short-circuit this ASP.NET request and end. Short-circuiting
          // prevents other modules from adding/interfering with the output.
          HttpContext.Current.ApplicationInstance.CompleteRequest();
          context.Response.End();
        }
      }   
    }

问题搞定。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值