add format file support (#2)

Co-authored-by: Long Ngo <long.ngo@vietgis.com.vn>
This commit is contained in:
Long Ngo 2021-09-21 22:17:33 +07:00 committed by GitHub
parent a9bbb90cdc
commit 67bed9519e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,24 +1,25 @@
using System; using System;
using System.Net.Http; using System.Net.Http;
using System.Text; using System.Text;
using System.Text.Json; using System.Text.Json;
using System.IO; using System.IO;
namespace QuickChart namespace QuickChart
{ {
internal class QuickChartShortUrlResponse internal class QuickChartShortUrlResponse
{ {
#pragma warning disable IDE1006 // Naming Styles #pragma warning disable IDE1006 // Naming Styles
public bool status { get; set; } public bool status { get; set; }
public string url { get; set; } public string url { get; set; }
#pragma warning restore IDE1006 // Naming Styles #pragma warning restore IDE1006 // Naming Styles
} }
public class Chart public class Chart
{ {
public int Width { get; set; } public int Width { get; set; }
public int Height { get; set; } public int Height { get; set; }
public double DevicePixelRatio { get; set; } public double DevicePixelRatio { get; set; }
public string Format {get;set;}
public string BackgroundColor { get; set; } public string BackgroundColor { get; set; }
public string Key { get; set; } public string Key { get; set; }
public string Config { get; set; } public string Config { get; set; }
@ -34,13 +35,14 @@ namespace QuickChart
Width = 500; Width = 500;
Height = 300; Height = 300;
DevicePixelRatio = 1.0; DevicePixelRatio = 1.0;
Format = "png";
BackgroundColor = "transparent"; BackgroundColor = "transparent";
Scheme = "https"; Scheme = "https";
Host = "quickchart.io"; Host = "quickchart.io";
Port = 443; Port = 443;
} }
public string GetUrl() public string GetUrl()
{ {
if (Config == null) if (Config == null)
@ -53,6 +55,7 @@ namespace QuickChart
builder.Append("&h=").Append(Height.ToString()); builder.Append("&h=").Append(Height.ToString());
builder.Append("&devicePixelRatio=").Append(DevicePixelRatio.ToString()); builder.Append("&devicePixelRatio=").Append(DevicePixelRatio.ToString());
builder.Append("&f=").Append(Format);
builder.Append("&bkg=").Append(Uri.EscapeDataString(BackgroundColor)); builder.Append("&bkg=").Append(Uri.EscapeDataString(BackgroundColor));
builder.Append("&c=").Append(Uri.EscapeDataString(Config)); builder.Append("&c=").Append(Uri.EscapeDataString(Config));
if (!string.IsNullOrEmpty(Key)) if (!string.IsNullOrEmpty(Key))
@ -61,8 +64,8 @@ namespace QuickChart
} }
return $"{Scheme}://{Host}:{Port}/chart?{builder}"; return $"{Scheme}://{Host}:{Port}/chart?{builder}";
} }
public string GetShortUrl() public string GetShortUrl()
{ {
if (Config == null) if (Config == null)
@ -79,6 +82,7 @@ namespace QuickChart
height = Height, height = Height,
backgroundColor = BackgroundColor, backgroundColor = BackgroundColor,
devicePixelRatio = DevicePixelRatio, devicePixelRatio = DevicePixelRatio,
format = Format,
chart = Config, chart = Config,
key = Key, key = Key,
}, options); }, options);
@ -98,8 +102,8 @@ namespace QuickChart
string responseText = response.Content.ReadAsStringAsync().Result; string responseText = response.Content.ReadAsStringAsync().Result;
QuickChartShortUrlResponse result = JsonSerializer.Deserialize<QuickChartShortUrlResponse>(responseText); QuickChartShortUrlResponse result = JsonSerializer.Deserialize<QuickChartShortUrlResponse>(responseText);
return result.url; return result.url;
} }
public byte[] ToByteArray() public byte[] ToByteArray()
{ {
if (Config == null) if (Config == null)
@ -116,6 +120,7 @@ namespace QuickChart
height = Height, height = Height,
backgroundColor = BackgroundColor, backgroundColor = BackgroundColor,
devicePixelRatio = DevicePixelRatio, devicePixelRatio = DevicePixelRatio,
format = Format,
chart = Config, chart = Config,
key = Key, key = Key,
}, options); }, options);
@ -138,6 +143,6 @@ namespace QuickChart
public void ToFile(string filePath) public void ToFile(string filePath)
{ {
File.WriteAllBytes(filePath, this.ToByteArray()); File.WriteAllBytes(filePath, this.ToByteArray());
} }
} }
} }