Compare commits

...
Sign in to create a new pull request.

11 commits

Author SHA1 Message Date
Mira
0b60f2e32d
chore: Update to net9 2025-01-27 00:02:49 +01:00
Mira
de08e5e4ee
feat: Expose API Response on Api Error 2023-09-21 16:52:24 +02:00
Ian Webster
491ab2d0ee
Add link to reference 2022-09-24 08:59:24 -07:00
Ian Webster
306a40d4d5 bump version to 2.3.0 2022-03-14 23:12:40 -07:00
dianaduca
03ac2f63d6
Add ability to specify chart generation url for self hosted QuickChart (#5)
* Add ability to specify chart generation url for self hosted QuickChart

* Add more configuration setting:

if user specify: a host only, default to https:443,
	         a host and http, set port to 80
	         a host and https, set port to 443

Co-authored-by: Duca Diana <dduca@globebmg.com>
2022-03-11 18:11:32 -08:00
Ian Webster
32d5ada399 Bump to version 2.2.0 2021-11-07 15:20:30 -08:00
Ian Webster
6bf090b391 Fix test 2021-11-07 15:20:21 -08:00
Ian Webster
3735d4d181
Add support for chart.js version parameter (#4) 2021-11-07 15:18:15 -08:00
Ian Webster
fbb6deee1a bump version 2021-09-21 08:37:04 -07:00
Ian Webster
f96e2a3adf formatting 2021-09-21 08:37:01 -07:00
Ian Webster
14c7cbb2ae
Add test for Format and update readme (#3) 2021-09-21 08:27:24 -07:00
5 changed files with 168 additions and 35 deletions

View file

@ -9,7 +9,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "QuickChartExamples", "QuickChartExamples\QuickChartExamples.csproj", "{52AC08E4-5A20-4A46-A117-BFAC03BB7940}" Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "QuickChartExamples", "QuickChartExamples\QuickChartExamples.csproj", "{52AC08E4-5A20-4A46-A117-BFAC03BB7940}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "QuickChartTest", "QuickChartTest\QuickChartTest.csproj", "{0CEF2268-6F2B-4DEB-B152-DDA0B09ABEDA}" Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "QuickChartTest", "QuickChartTest\QuickChartTest.csproj", "{21C67AF9-6B21-4FA3-A203-294C36DC7F2A}"
EndProject EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
@ -25,10 +25,10 @@ Global
{52AC08E4-5A20-4A46-A117-BFAC03BB7940}.Debug|Any CPU.Build.0 = Debug|Any CPU {52AC08E4-5A20-4A46-A117-BFAC03BB7940}.Debug|Any CPU.Build.0 = Debug|Any CPU
{52AC08E4-5A20-4A46-A117-BFAC03BB7940}.Release|Any CPU.ActiveCfg = Release|Any CPU {52AC08E4-5A20-4A46-A117-BFAC03BB7940}.Release|Any CPU.ActiveCfg = Release|Any CPU
{52AC08E4-5A20-4A46-A117-BFAC03BB7940}.Release|Any CPU.Build.0 = Release|Any CPU {52AC08E4-5A20-4A46-A117-BFAC03BB7940}.Release|Any CPU.Build.0 = Release|Any CPU
{0CEF2268-6F2B-4DEB-B152-DDA0B09ABEDA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {21C67AF9-6B21-4FA3-A203-294C36DC7F2A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{0CEF2268-6F2B-4DEB-B152-DDA0B09ABEDA}.Debug|Any CPU.Build.0 = Debug|Any CPU {21C67AF9-6B21-4FA3-A203-294C36DC7F2A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0CEF2268-6F2B-4DEB-B152-DDA0B09ABEDA}.Release|Any CPU.ActiveCfg = Release|Any CPU {21C67AF9-6B21-4FA3-A203-294C36DC7F2A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0CEF2268-6F2B-4DEB-B152-DDA0B09ABEDA}.Release|Any CPU.Build.0 = Release|Any CPU {21C67AF9-6B21-4FA3-A203-294C36DC7F2A}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE

View file

@ -22,6 +22,7 @@ namespace QuickChart
public string Format { 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 Version { get; set; }
public string Config { get; set; } public string Config { get; set; }
public string Scheme { get; set; } public string Scheme { get; set; }
@ -30,7 +31,7 @@ namespace QuickChart
private static readonly HttpClient Client = new HttpClient(); private static readonly HttpClient Client = new HttpClient();
public Chart() public Chart(string scheme = null, string host = null, int? port = null)
{ {
Width = 500; Width = 500;
Height = 300; Height = 300;
@ -38,10 +39,41 @@ namespace QuickChart
Format = "png"; Format = "png";
BackgroundColor = "transparent"; BackgroundColor = "transparent";
if (host != null)
{
Host = host;
if (scheme != null)
{
Scheme = scheme;
if (port.HasValue)
{
Port = port.Value;
}
else
{
if(scheme == "http")
{
Port = 80;
}
else
{
Port = 443;
}
}
}
else
{
Scheme = "https";
Port = 443;
}
}
else
{
Scheme = "https"; Scheme = "https";
Host = "quickchart.io"; Host = "quickchart.io";
Port = 443; Port = 443;
} }
}
public string GetUrl() public string GetUrl()
{ {
@ -62,6 +94,10 @@ namespace QuickChart
{ {
builder.Append("&key=").Append(Uri.EscapeDataString(Key)); builder.Append("&key=").Append(Uri.EscapeDataString(Key));
} }
if (!string.IsNullOrEmpty(Version))
{
builder.Append("&v=").Append(Uri.EscapeDataString(Version));
}
return $"{Scheme}://{Host}:{Port}/chart?{builder}"; return $"{Scheme}://{Host}:{Port}/chart?{builder}";
} }
@ -85,6 +121,7 @@ namespace QuickChart
format = Format, format = Format,
chart = Config, chart = Config,
key = Key, key = Key,
version = Version,
}, options); }, options);
string url = $"{Scheme}://{Host}:{Port}/chart/create"; string url = $"{Scheme}://{Host}:{Port}/chart/create";
@ -96,7 +133,7 @@ namespace QuickChart
if (!response.IsSuccessStatusCode) if (!response.IsSuccessStatusCode)
{ {
throw new Exception("Unsuccessful response from API"); throw new ApiException("Unsuccessful response from API", response);
} }
string responseText = response.Content.ReadAsStringAsync().Result; string responseText = response.Content.ReadAsStringAsync().Result;
@ -123,6 +160,7 @@ namespace QuickChart
format = Format, format = Format,
chart = Config, chart = Config,
key = Key, key = Key,
version = Version,
}, options); }, options);
string url = $"{Scheme}://{Host}:{Port}/chart"; string url = $"{Scheme}://{Host}:{Port}/chart";
@ -134,7 +172,7 @@ namespace QuickChart
if (!response.IsSuccessStatusCode) if (!response.IsSuccessStatusCode)
{ {
throw new Exception("Unsuccessful response from API"); throw new ApiException("Unsuccessful response from API", response);
} }
return response.Content.ReadAsByteArrayAsync().Result; return response.Content.ReadAsByteArrayAsync().Result;
@ -145,4 +183,12 @@ namespace QuickChart
File.WriteAllBytes(filePath, this.ToByteArray()); File.WriteAllBytes(filePath, this.ToByteArray());
} }
} }
public class ApiException : Exception
{
public ApiException(string message, HttpResponseMessage response) : base(message)
=> this.Response = response;
public HttpResponseMessage Response { get; private set; }
}
} }

View file

@ -2,11 +2,10 @@
<PropertyGroup> <PropertyGroup>
<OutputType>Library</OutputType> <OutputType>Library</OutputType>
<TargetFrameworks>netcoreapp3.1;netstandard2.0;net472</TargetFrameworks> <TargetFrameworks>net9.0</TargetFrameworks>
<PackOnBuild>true</PackOnBuild> <PackOnBuild>true</PackOnBuild>
<Authors>Ian Webster</Authors> <Authors>Ian Webster</Authors>
<Copyright>Ian Webster 2020</Copyright> <Copyright>Ian Webster</Copyright>
<PackageLicenseUrl>https://github.com/typpo/quickchart-csharp/blob/main/LICENSE</PackageLicenseUrl>
<PackageLicenseExpression>MIT</PackageLicenseExpression> <PackageLicenseExpression>MIT</PackageLicenseExpression>
<Owners>QuickChart</Owners> <Owners>QuickChart</Owners>
<PackageProjectUrl>https://github.com/typpo/quickchart-csharp</PackageProjectUrl> <PackageProjectUrl>https://github.com/typpo/quickchart-csharp</PackageProjectUrl>
@ -14,7 +13,7 @@
<PackageTags>chart image, chart api, chart, charts, image charts</PackageTags> <PackageTags>chart image, chart api, chart, charts, image charts</PackageTags>
<Title>QuickChart</Title> <Title>QuickChart</Title>
<Description>Client library for the QuickChart Chart API, used to generate chart images.</Description> <Description>Client library for the QuickChart Chart API, used to generate chart images.</Description>
<PackageVersion>2.0.0</PackageVersion> <PackageVersion>2.3.0</PackageVersion>
<PackageId>QuickChart</PackageId> <PackageId>QuickChart</PackageId>
</PropertyGroup> </PropertyGroup>

View file

@ -59,5 +59,86 @@ namespace QuickChartTest
Assert.Contains("h=300", url); Assert.Contains("h=300", url);
Assert.Contains("key=abc123", url); Assert.Contains("key=abc123", url);
} }
[Fact]
public void TestWithFormat()
{
Chart qc = new Chart
{
Width = 500,
Height = 300,
Format = "svg",
Config = @"{
type: 'bar',
data: {
labels: ['Q1', 'Q2', 'Q3', 'Q4'],
datasets: [{
label: 'Users',
data: [50, 60, 70, 180]
}]
}
}"
};
string url = qc.GetUrl();
Assert.Contains("https://quickchart.io:443/chart", url);
Assert.Contains("w=500", url);
Assert.Contains("h=300", url);
Assert.Contains("f=svg", url);
}
[Fact]
public void TestWithVersion()
{
Chart qc = new Chart
{
Width = 500,
Height = 300,
Version = "2.9.4",
Config = @"{
type: 'bar',
data: {
labels: ['Q1', 'Q2', 'Q3', 'Q4'],
datasets: [{
label: 'Users',
data: [50, 60, 70, 180]
}]
}
}"
};
string url = qc.GetUrl();
Assert.Contains("https://quickchart.io:443/chart", url);
Assert.Contains("w=500", url);
Assert.Contains("h=300", url);
Assert.Contains("v=2.9.4", url);
}
[Fact]
public void TestWithSelfHostingQuickChart()
{
var scheme = "http";
var host = "localhost";
var port = 47000;
Chart qc = new Chart(scheme, host, port);
qc.Width = 500;
qc.Height = 300;
qc.Config = @"{
type: 'bar',
data: {
labels: ['Q1', 'Q2', 'Q3', 'Q4'],
datasets: [{
label: 'Users',
data: [50, 60, 70, 180]
}]
}[]
}";
string url = qc.GetUrl();
Assert.Contains($"{scheme}://{host}:{port}/chart", url);
Assert.Contains("w=500", url);
Assert.Contains("h=300", url);
Assert.DoesNotContain("key=", url);
}
} }
} }

View file

@ -10,11 +10,11 @@ A C# client for the [quickchart.io](https://quickchart.io/) chart API.
Use `QuickChart/QuickChart.cs` in this project, or install the `QuickChart` package from [NuGet](https://www.nuget.org/packages/QuickChart): Use `QuickChart/QuickChart.cs` in this project, or install the `QuickChart` package from [NuGet](https://www.nuget.org/packages/QuickChart):
``` ```
PM> Install-Package QuickChart -Version 2.0.0 PM> Install-Package QuickChart -Version 2.1.0
``` ```
or or
``` ```
dotnet add package QuickChart --version 2.0.0 dotnet add package QuickChart --version 2.1.0
``` ```
--- ---
## Usage ## Usage
@ -26,6 +26,7 @@ Chart qc = new Chart();
qc.Width = 500; qc.Width = 500;
qc.Height = 300; qc.Height = 300;
qc.Version = "2.9.4";
qc.Config = @"{ qc.Config = @"{
type: 'bar', type: 'bar',
data: { data: {
@ -77,6 +78,12 @@ The background color of the chart. Any valid HTML color works. Defaults to #ffff
#### DevicePixelRatio: double #### DevicePixelRatio: double
The device pixel ratio of the chart. This will multiply the number of pixels by the value. This is usually used for retina displays. Defaults to 1.0. The device pixel ratio of the chart. This will multiply the number of pixels by the value. This is usually used for retina displays. Defaults to 1.0.
#### Format: string
The output format of the chart. Defaults to "png"
#### Version: string
Chart.js version. See [documentation](https://quickchart.io/documentation/#parameters) for supported versions.
#### Key: string #### Key: string
API key (not required) API key (not required)