Compare commits

..

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 123 additions and 20 deletions

View file

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

View file

@ -19,9 +19,10 @@ namespace QuickChart
public int Width { get; set; }
public int Height { get; set; }
public double DevicePixelRatio { get; set; }
public string Format {get;set;}
public string Format { get; set; }
public string BackgroundColor { get; set; }
public string Key { get; set; }
public string Version { get; set; }
public string Config { get; set; }
public string Scheme { get; set; }
@ -30,7 +31,7 @@ namespace QuickChart
private static readonly HttpClient Client = new HttpClient();
public Chart()
public Chart(string scheme = null, string host = null, int? port = null)
{
Width = 500;
Height = 300;
@ -38,9 +39,40 @@ namespace QuickChart
Format = "png";
BackgroundColor = "transparent";
Scheme = "https";
Host = "quickchart.io";
Port = 443;
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";
Host = "quickchart.io";
Port = 443;
}
}
public string GetUrl()
@ -62,6 +94,10 @@ namespace QuickChart
{
builder.Append("&key=").Append(Uri.EscapeDataString(Key));
}
if (!string.IsNullOrEmpty(Version))
{
builder.Append("&v=").Append(Uri.EscapeDataString(Version));
}
return $"{Scheme}://{Host}:{Port}/chart?{builder}";
}
@ -85,6 +121,7 @@ namespace QuickChart
format = Format,
chart = Config,
key = Key,
version = Version,
}, options);
string url = $"{Scheme}://{Host}:{Port}/chart/create";
@ -96,7 +133,7 @@ namespace QuickChart
if (!response.IsSuccessStatusCode)
{
throw new Exception("Unsuccessful response from API");
throw new ApiException("Unsuccessful response from API", response);
}
string responseText = response.Content.ReadAsStringAsync().Result;
@ -123,6 +160,7 @@ namespace QuickChart
format = Format,
chart = Config,
key = Key,
version = Version,
}, options);
string url = $"{Scheme}://{Host}:{Port}/chart";
@ -134,7 +172,7 @@ namespace QuickChart
if (!response.IsSuccessStatusCode)
{
throw new Exception("Unsuccessful response from API");
throw new ApiException("Unsuccessful response from API", response);
}
return response.Content.ReadAsByteArrayAsync().Result;
@ -145,4 +183,12 @@ namespace QuickChart
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>
<OutputType>Library</OutputType>
<TargetFrameworks>netcoreapp3.1;netstandard2.0;net472</TargetFrameworks>
<TargetFrameworks>net9.0</TargetFrameworks>
<PackOnBuild>true</PackOnBuild>
<Authors>Ian Webster</Authors>
<Copyright>Ian Webster 2020</Copyright>
<PackageLicenseUrl>https://github.com/typpo/quickchart-csharp/blob/main/LICENSE</PackageLicenseUrl>
<Copyright>Ian Webster</Copyright>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<Owners>QuickChart</Owners>
<PackageProjectUrl>https://github.com/typpo/quickchart-csharp</PackageProjectUrl>
@ -14,7 +13,7 @@
<PackageTags>chart image, chart api, chart, charts, image charts</PackageTags>
<Title>QuickChart</Title>
<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>
</PropertyGroup>

View file

@ -86,5 +86,59 @@ namespace QuickChartTest
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):
```
PM> Install-Package QuickChart -Version 2.0.0
PM> Install-Package QuickChart -Version 2.1.0
```
or
```
dotnet add package QuickChart --version 2.0.0
dotnet add package QuickChart --version 2.1.0
```
---
## Usage
@ -26,13 +26,14 @@ Chart qc = new Chart();
qc.Width = 500;
qc.Height = 300;
qc.Version = "2.9.4";
qc.Config = @"{
type: 'bar',
data: {
labels: ['Q1', 'Q2', 'Q3', 'Q4'],
datasets: [{
label: 'Users',
data: [50, 60, 70, 180]
label: 'Users',
data: [50, 60, 70, 180]
}]
}
}";
@ -80,6 +81,9 @@ The device pixel ratio of the chart. This will multiply the number of pixels by
#### 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
API key (not required)