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.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<QuickChartShortUrlResponse>(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());
}
}
}
}
}
}