From 67bed9519e0909f5aca4a4fac1842ff295bc73ee Mon Sep 17 00:00:00 2001 From: Long Ngo Date: Tue, 21 Sep 2021 22:17:33 +0700 Subject: [PATCH] add format file support (#2) Co-authored-by: Long Ngo --- QuickChart/QuickChart.cs | 39 ++++++++++++++++++++++----------------- 1 file changed, 22 insertions(+), 17 deletions(-) diff --git a/QuickChart/QuickChart.cs b/QuickChart/QuickChart.cs index d0dffbf..eb03464 100644 --- a/QuickChart/QuickChart.cs +++ b/QuickChart/QuickChart.cs @@ -1,24 +1,25 @@ -using System; -using System.Net.Http; +using System; +using System.Net.Http; using System.Text; using System.Text.Json; using System.IO; -namespace QuickChart -{ +namespace QuickChart +{ internal class QuickChartShortUrlResponse { #pragma warning disable IDE1006 // Naming Styles public bool status { get; set; } public string url { get; set; } #pragma warning restore IDE1006 // Naming Styles - } - - public class Chart - { + } + + public class Chart + { public int Width { get; set; } public int Height { get; set; } public double DevicePixelRatio { get; set; } + public string Format {get;set;} public string BackgroundColor { get; set; } public string Key { get; set; } public string Config { get; set; } @@ -34,13 +35,14 @@ namespace QuickChart Width = 500; Height = 300; DevicePixelRatio = 1.0; + Format = "png"; BackgroundColor = "transparent"; Scheme = "https"; Host = "quickchart.io"; Port = 443; - } - + } + public string GetUrl() { if (Config == null) @@ -53,6 +55,7 @@ namespace QuickChart builder.Append("&h=").Append(Height.ToString()); builder.Append("&devicePixelRatio=").Append(DevicePixelRatio.ToString()); + builder.Append("&f=").Append(Format); builder.Append("&bkg=").Append(Uri.EscapeDataString(BackgroundColor)); builder.Append("&c=").Append(Uri.EscapeDataString(Config)); if (!string.IsNullOrEmpty(Key)) @@ -61,8 +64,8 @@ namespace QuickChart } return $"{Scheme}://{Host}:{Port}/chart?{builder}"; - } - + } + public string GetShortUrl() { if (Config == null) @@ -79,6 +82,7 @@ namespace QuickChart height = Height, backgroundColor = BackgroundColor, devicePixelRatio = DevicePixelRatio, + format = Format, chart = Config, key = Key, }, options); @@ -98,8 +102,8 @@ namespace QuickChart string responseText = response.Content.ReadAsStringAsync().Result; QuickChartShortUrlResponse result = JsonSerializer.Deserialize(responseText); return result.url; - } - + } + public byte[] ToByteArray() { if (Config == null) @@ -116,6 +120,7 @@ namespace QuickChart height = Height, backgroundColor = BackgroundColor, devicePixelRatio = DevicePixelRatio, + format = Format, chart = Config, key = Key, }, options); @@ -138,6 +143,6 @@ namespace QuickChart public void ToFile(string filePath) { File.WriteAllBytes(filePath, this.ToByteArray()); - } - } -} + } + } +}