EChart 后台生成图片思路

https://www.echartsjs.com/zh/tutorial.html#5%20%E5%88%86%E9%92%9F%E4%B8%8A%E6%89%8B%20ECharts
echart 学习页面


参考思路, 因为 Echart不支持直接生成图片:
1. 写一个generateChart.html页面 接受参数 sf=sd&date=20200208 , 根据省份(sf) 日期(date) 生成对应的折线图
2. 在generateChart.html 页面中ajax 调用后台接口 saveImage.ashx 生成某省图片png

var myChart = ec.init(document.getElementById('myChart'));
var data = "ImageData=" + encodeURI(myChart.getDataURL())+ "sf=sd&date=20200208"; //这里应该动态
$.ajax({
type: "post",
url: "/ajax/saveImageHandler.ashx",
data: "action=saveImage&" + data,
cache: false,
success: function (msg) {
}
});
});

3. 获取 ImageData 的base64生成图片
***参考地址:
c#
https://www.cnblogs.com/wdkshy/p/4206018.html
java
https://blog.csdn.net/tjj3027/article/details/80421170

public class saveImageHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
//生成图片
SaveImage()
context.Response.Clear();
context.Response.ContentType = "text/plain";
//输出 json 字符串
context.Response.Write("{isSucess:true}");
context.Response.End();
}
}

public bool IsReusable
{
get
{
return false;
}
}

protected void SaveImage()
{
int returnid = 0;
string ImageData = Request.Form["ImageData"];
ImageData=Server.UrlDecode(ImageData);
ImageData = ImageData.Replace(" ", "+");

//string sf= Request.Form["sf"]; //省份等。。。。。
try {
string[] url = ImageData.Split(',');
string u = url[1];
// Base64解码
byte[] b = Convert.FromBase64String(u);
string name = "E:\\sd20200209.png";
FileHelper.ByteStreamToFile(name, b);
} catch (Exception e) {
}}

}

 

//二进制数组Byte[]生成文件
public static bool ByteStreamToFile(string createFileFullPath, byte[] streamByte)
{
if (!File.Exists(createFileFullPath))
{
FileStream fileStream = File.Create(createFileFullPath);
fileStream.Write(streamByte, 0, streamByte.Length);
fileStream.Close();
return true;
}
return false;
}

}
4. 在 Global.asax.cs 文件中添加一个 Timer 对象定时调用全国31个省份,日期分别生成
generateChart.html?sf=sd&date=20200208
generateChart.html?sf=sh&date=20200208
* 模拟post请求页面实例 generateChart.html?sf=XX&date=XXXXXXX
https://www.jb51.net/article/86067.htm

private string HttpPost(string Url, string postDataStr)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = Encoding.UTF8.GetByteCount(postDataStr);
request.CookieContainer = cookie;
Stream myRequestStream = request.GetRequestStream();
StreamWriter myStreamWriter = new StreamWriter(myRequestStream, Encoding.GetEncoding("gb2312"));
myStreamWriter.Write(postDataStr);
myStreamWriter.Close();

HttpWebResponse response = (HttpWebResponse)request.GetResponse();

response.Cookies = cookie.GetCookies(response.ResponseUri);
Stream myResponseStream = response.GetResponseStream();
StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
string retString = myStreamReader.ReadToEnd();
myStreamReader.Close();
myResponseStream.Close();

return retString;

你可能感兴趣的:(EChart 后台生成图片思路)