├── .gitignore ├── .travis.yml ├── LICENSE ├── QuickChart.sln ├── QuickChart ├── QuickChart.cs └── QuickChart.csproj ├── QuickChartExamples ├── QuickChartExamples.csproj ├── ShortUrl.cs ├── Simple.cs └── ToFile.cs ├── QuickChartTest ├── QuickChartTest.cs └── QuickChartTest.csproj └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | # globs 2 | Makefile.in 3 | *.userprefs 4 | *.usertasks 5 | config.make 6 | config.status 7 | aclocal.m4 8 | install-sh 9 | autom4te.cache/ 10 | *.tar.gz 11 | tarballs/ 12 | test-results/ 13 | 14 | # Mac bundle stuff 15 | *.dmg 16 | *.app 17 | 18 | # content below from: https://github.com/github/gitignore/blob/main/Global/macOS.gitignore 19 | # General 20 | .DS_Store 21 | .AppleDouble 22 | .LSOverride 23 | 24 | # Icon must end with two \r 25 | Icon 26 | 27 | 28 | # Thumbnails 29 | ._* 30 | 31 | # Files that might appear in the root of a volume 32 | .DocumentRevisions-V100 33 | .fseventsd 34 | .Spotlight-V100 35 | .TemporaryItems 36 | .Trashes 37 | .VolumeIcon.icns 38 | .com.apple.timemachine.donotpresent 39 | 40 | # Directories potentially created on remote AFP share 41 | .AppleDB 42 | .AppleDesktop 43 | Network Trash Folder 44 | Temporary Items 45 | .apdisk 46 | 47 | # content below from: https://github.com/github/gitignore/blob/main/Global/Windows.gitignore 48 | # Windows thumbnail cache files 49 | Thumbs.db 50 | ehthumbs.db 51 | ehthumbs_vista.db 52 | 53 | # Dump file 54 | *.stackdump 55 | 56 | # Folder config file 57 | [Dd]esktop.ini 58 | 59 | # Recycle Bin used on file shares 60 | $RECYCLE.BIN/ 61 | 62 | # Windows Installer files 63 | *.cab 64 | *.msi 65 | *.msix 66 | *.msm 67 | *.msp 68 | 69 | # Windows shortcuts 70 | *.lnk 71 | 72 | # content below from: https://github.com/github/gitignore/blob/master/VisualStudio.gitignore 73 | ## Ignore Visual Studio temporary files, build results, and 74 | ## files generated by popular Visual Studio add-ons. 75 | ## 76 | ## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore 77 | 78 | # User-specific files 79 | *.suo 80 | *.user 81 | *.userosscache 82 | *.sln.docstates 83 | 84 | # User-specific files (MonoDevelop/Xamarin Studio) 85 | *.userprefs 86 | 87 | # Build results 88 | [Dd]ebug/ 89 | [Dd]ebugPublic/ 90 | [Rr]elease/ 91 | [Rr]eleases/ 92 | x64/ 93 | x86/ 94 | bld/ 95 | [Bb]in/ 96 | [Oo]bj/ 97 | [Ll]og/ 98 | 99 | # Visual Studio 2015/2017 cache/options directory 100 | .vs/ 101 | # Uncomment if you have tasks that create the project's static files in wwwroot 102 | #wwwroot/ 103 | 104 | # Visual Studio 2017 auto generated files 105 | Generated\ Files/ 106 | 107 | # MSTest test Results 108 | [Tt]est[Rr]esult*/ 109 | [Bb]uild[Ll]og.* 110 | 111 | # NUNIT 112 | *.VisualState.xml 113 | TestResult.xml 114 | 115 | # Build Results of an ATL Project 116 | [Dd]ebugPS/ 117 | [Rr]eleasePS/ 118 | dlldata.c 119 | 120 | # Benchmark Results 121 | BenchmarkDotNet.Artifacts/ 122 | 123 | # .NET Core 124 | project.lock.json 125 | project.fragment.lock.json 126 | artifacts/ 127 | 128 | # StyleCop 129 | StyleCopReport.xml 130 | 131 | # Files built by Visual Studio 132 | *_i.c 133 | *_p.c 134 | *_h.h 135 | *.ilk 136 | *.meta 137 | *.obj 138 | *.iobj 139 | *.pch 140 | *.pdb 141 | *.ipdb 142 | *.pgc 143 | *.pgd 144 | *.rsp 145 | *.sbr 146 | *.tlb 147 | *.tli 148 | *.tlh 149 | *.tmp 150 | *.tmp_proj 151 | *_wpftmp.csproj 152 | *.log 153 | *.vspscc 154 | *.vssscc 155 | .builds 156 | *.pidb 157 | *.svclog 158 | *.scc 159 | 160 | # Chutzpah Test files 161 | _Chutzpah* 162 | 163 | # Visual C++ cache files 164 | ipch/ 165 | *.aps 166 | *.ncb 167 | *.opendb 168 | *.opensdf 169 | *.sdf 170 | *.cachefile 171 | *.VC.db 172 | *.VC.VC.opendb 173 | 174 | # Visual Studio profiler 175 | *.psess 176 | *.vsp 177 | *.vspx 178 | *.sap 179 | 180 | # Visual Studio Trace Files 181 | *.e2e 182 | 183 | # TFS 2012 Local Workspace 184 | $tf/ 185 | 186 | # Guidance Automation Toolkit 187 | *.gpState 188 | 189 | # ReSharper is a .NET coding add-in 190 | _ReSharper*/ 191 | *.[Rr]e[Ss]harper 192 | *.DotSettings.user 193 | 194 | # JustCode is a .NET coding add-in 195 | .JustCode 196 | 197 | # TeamCity is a build add-in 198 | _TeamCity* 199 | 200 | # DotCover is a Code Coverage Tool 201 | *.dotCover 202 | 203 | # AxoCover is a Code Coverage Tool 204 | .axoCover/* 205 | !.axoCover/settings.json 206 | 207 | # Visual Studio code coverage results 208 | *.coverage 209 | *.coveragexml 210 | 211 | # NCrunch 212 | _NCrunch_* 213 | .*crunch*.local.xml 214 | nCrunchTemp_* 215 | 216 | # MightyMoose 217 | *.mm.* 218 | AutoTest.Net/ 219 | 220 | # Web workbench (sass) 221 | .sass-cache/ 222 | 223 | # Installshield output folder 224 | [Ee]xpress/ 225 | 226 | # DocProject is a documentation generator add-in 227 | DocProject/buildhelp/ 228 | DocProject/Help/*.HxT 229 | DocProject/Help/*.HxC 230 | DocProject/Help/*.hhc 231 | DocProject/Help/*.hhk 232 | DocProject/Help/*.hhp 233 | DocProject/Help/Html2 234 | DocProject/Help/html 235 | 236 | # Click-Once directory 237 | publish/ 238 | 239 | # Publish Web Output 240 | *.[Pp]ublish.xml 241 | *.azurePubxml 242 | # Note: Comment the next line if you want to checkin your web deploy settings, 243 | # but database connection strings (with potential passwords) will be unencrypted 244 | *.pubxml 245 | *.publishproj 246 | 247 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 248 | # checkin your Azure Web App publish settings, but sensitive information contained 249 | # in these scripts will be unencrypted 250 | PublishScripts/ 251 | 252 | # NuGet Packages 253 | *.nupkg 254 | # The packages folder can be ignored because of Package Restore 255 | **/[Pp]ackages/* 256 | # except build/, which is used as an MSBuild target. 257 | !**/[Pp]ackages/build/ 258 | # Uncomment if necessary however generally it will be regenerated when needed 259 | #!**/[Pp]ackages/repositories.config 260 | # NuGet v3's project.json files produces more ignorable files 261 | *.nuget.props 262 | *.nuget.targets 263 | 264 | # Microsoft Azure Build Output 265 | csx/ 266 | *.build.csdef 267 | 268 | # Microsoft Azure Emulator 269 | ecf/ 270 | rcf/ 271 | 272 | # Windows Store app package directories and files 273 | AppPackages/ 274 | BundleArtifacts/ 275 | Package.StoreAssociation.xml 276 | _pkginfo.txt 277 | *.appx 278 | 279 | # Visual Studio cache files 280 | # files ending in .cache can be ignored 281 | *.[Cc]ache 282 | # but keep track of directories ending in .cache 283 | !*.[Cc]ache/ 284 | 285 | # Others 286 | ClientBin/ 287 | ~$* 288 | *~ 289 | *.dbmdl 290 | *.dbproj.schemaview 291 | *.jfm 292 | *.pfx 293 | *.publishsettings 294 | orleans.codegen.cs 295 | 296 | # Including strong name files can present a security risk 297 | # (https://github.com/github/gitignore/pull/2483#issue-259490424) 298 | #*.snk 299 | 300 | # Since there are multiple workflows, uncomment next line to ignore bower_components 301 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 302 | #bower_components/ 303 | 304 | # RIA/Silverlight projects 305 | Generated_Code/ 306 | 307 | # Backup & report files from converting an old project file 308 | # to a newer Visual Studio version. Backup files are not needed, 309 | # because we have git ;-) 310 | _UpgradeReport_Files/ 311 | Backup*/ 312 | UpgradeLog*.XML 313 | UpgradeLog*.htm 314 | ServiceFabricBackup/ 315 | *.rptproj.bak 316 | 317 | # SQL Server files 318 | *.mdf 319 | *.ldf 320 | *.ndf 321 | 322 | # Business Intelligence projects 323 | *.rdl.data 324 | *.bim.layout 325 | *.bim_*.settings 326 | *.rptproj.rsuser 327 | 328 | # Microsoft Fakes 329 | FakesAssemblies/ 330 | 331 | # GhostDoc plugin setting file 332 | *.GhostDoc.xml 333 | 334 | # Node.js Tools for Visual Studio 335 | .ntvs_analysis.dat 336 | node_modules/ 337 | 338 | # Visual Studio 6 build log 339 | *.plg 340 | 341 | # Visual Studio 6 workspace options file 342 | *.opt 343 | 344 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.) 345 | *.vbw 346 | 347 | # Visual Studio LightSwitch build output 348 | **/*.HTMLClient/GeneratedArtifacts 349 | **/*.DesktopClient/GeneratedArtifacts 350 | **/*.DesktopClient/ModelManifest.xml 351 | **/*.Server/GeneratedArtifacts 352 | **/*.Server/ModelManifest.xml 353 | _Pvt_Extensions 354 | 355 | # Paket dependency manager 356 | .paket/paket.exe 357 | paket-files/ 358 | 359 | # FAKE - F# Make 360 | .fake/ 361 | 362 | # JetBrains Rider 363 | .idea/ 364 | *.sln.iml 365 | 366 | # CodeRush personal settings 367 | .cr/personal 368 | 369 | # Python Tools for Visual Studio (PTVS) 370 | __pycache__/ 371 | *.pyc 372 | 373 | # Cake - Uncomment if you are using it 374 | # tools/** 375 | # !tools/packages.config 376 | 377 | # Tabs Studio 378 | *.tss 379 | 380 | # Telerik's JustMock configuration file 381 | *.jmconfig 382 | 383 | # BizTalk build output 384 | *.btp.cs 385 | *.btm.cs 386 | *.odx.cs 387 | *.xsd.cs 388 | 389 | # OpenCover UI analysis results 390 | OpenCover/ 391 | 392 | # Azure Stream Analytics local run output 393 | ASALocalRun/ 394 | 395 | # MSBuild Binary and Structured Log 396 | *.binlog 397 | 398 | # NVidia Nsight GPU debugger configuration file 399 | *.nvuser 400 | 401 | # MFractors (Xamarin productivity tool) working folder 402 | .mfractor/ 403 | 404 | # Local History for Visual Studio 405 | .localhistory/ -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: csharp 2 | solution: QuickChart.sln 3 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Ian Webster 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /QuickChart.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio Version 16 4 | VisualStudioVersion = 16.0.808.2 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "QuickChart", "QuickChart\QuickChart.csproj", "{60C2308A-500A-4DA1-AF8E-671C88696BFA}" 7 | EndProject 8 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{C076DB54-F991-4386-969E-110648801D22}" 9 | EndProject 10 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "QuickChartExamples", "QuickChartExamples\QuickChartExamples.csproj", "{52AC08E4-5A20-4A46-A117-BFAC03BB7940}" 11 | EndProject 12 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "QuickChartTest", "QuickChartTest\QuickChartTest.csproj", "{21C67AF9-6B21-4FA3-A203-294C36DC7F2A}" 13 | EndProject 14 | Global 15 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 16 | Debug|Any CPU = Debug|Any CPU 17 | Release|Any CPU = Release|Any CPU 18 | EndGlobalSection 19 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 20 | {60C2308A-500A-4DA1-AF8E-671C88696BFA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 21 | {60C2308A-500A-4DA1-AF8E-671C88696BFA}.Debug|Any CPU.Build.0 = Debug|Any CPU 22 | {60C2308A-500A-4DA1-AF8E-671C88696BFA}.Release|Any CPU.ActiveCfg = Release|Any CPU 23 | {60C2308A-500A-4DA1-AF8E-671C88696BFA}.Release|Any CPU.Build.0 = Release|Any CPU 24 | {52AC08E4-5A20-4A46-A117-BFAC03BB7940}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 25 | {52AC08E4-5A20-4A46-A117-BFAC03BB7940}.Debug|Any CPU.Build.0 = Debug|Any CPU 26 | {52AC08E4-5A20-4A46-A117-BFAC03BB7940}.Release|Any CPU.ActiveCfg = Release|Any CPU 27 | {52AC08E4-5A20-4A46-A117-BFAC03BB7940}.Release|Any CPU.Build.0 = Release|Any CPU 28 | {21C67AF9-6B21-4FA3-A203-294C36DC7F2A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 29 | {21C67AF9-6B21-4FA3-A203-294C36DC7F2A}.Debug|Any CPU.Build.0 = Debug|Any CPU 30 | {21C67AF9-6B21-4FA3-A203-294C36DC7F2A}.Release|Any CPU.ActiveCfg = Release|Any CPU 31 | {21C67AF9-6B21-4FA3-A203-294C36DC7F2A}.Release|Any CPU.Build.0 = Release|Any CPU 32 | EndGlobalSection 33 | GlobalSection(SolutionProperties) = preSolution 34 | HideSolutionNode = FALSE 35 | EndGlobalSection 36 | GlobalSection(ExtensibilityGlobals) = postSolution 37 | SolutionGuid = {7CE792CD-B43F-4CD6-8FBF-4D55C2A07BC6} 38 | EndGlobalSection 39 | EndGlobal 40 | -------------------------------------------------------------------------------- /QuickChart/QuickChart.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Net.Http; 3 | using System.Text; 4 | using System.Text.Json; 5 | using System.IO; 6 | 7 | namespace QuickChart 8 | { 9 | internal class QuickChartShortUrlResponse 10 | { 11 | #pragma warning disable IDE1006 // Naming Styles 12 | public bool status { get; set; } 13 | public string url { get; set; } 14 | #pragma warning restore IDE1006 // Naming Styles 15 | } 16 | 17 | public class Chart 18 | { 19 | public int Width { get; set; } 20 | public int Height { get; set; } 21 | public double DevicePixelRatio { get; set; } 22 | public string Format { get; set; } 23 | public string BackgroundColor { get; set; } 24 | public string Key { get; set; } 25 | public string Version { get; set; } 26 | public string Config { get; set; } 27 | 28 | public string Scheme { get; set; } 29 | public string Host { get; set; } 30 | public int Port { get; set; } 31 | 32 | private static readonly HttpClient Client = new HttpClient(); 33 | 34 | public Chart(string scheme = null, string host = null, int? port = null) 35 | { 36 | Width = 500; 37 | Height = 300; 38 | DevicePixelRatio = 1.0; 39 | Format = "png"; 40 | BackgroundColor = "transparent"; 41 | 42 | if (host != null) 43 | { 44 | Host = host; 45 | if (scheme != null) 46 | { 47 | Scheme = scheme; 48 | if (port.HasValue) 49 | { 50 | Port = port.Value; 51 | } 52 | else 53 | { 54 | if(scheme == "http") 55 | { 56 | Port = 80; 57 | } 58 | else 59 | { 60 | Port = 443; 61 | } 62 | } 63 | } 64 | else 65 | { 66 | Scheme = "https"; 67 | Port = 443; 68 | } 69 | } 70 | else 71 | { 72 | Scheme = "https"; 73 | Host = "quickchart.io"; 74 | Port = 443; 75 | } 76 | } 77 | 78 | public string GetUrl() 79 | { 80 | if (Config == null) 81 | { 82 | throw new NullReferenceException("You must set Config on the QuickChart object before generating a URL"); 83 | } 84 | 85 | StringBuilder builder = new StringBuilder(); 86 | builder.Append("w=").Append(Width.ToString()); 87 | builder.Append("&h=").Append(Height.ToString()); 88 | 89 | builder.Append("&devicePixelRatio=").Append(DevicePixelRatio.ToString()); 90 | builder.Append("&f=").Append(Format); 91 | builder.Append("&bkg=").Append(Uri.EscapeDataString(BackgroundColor)); 92 | builder.Append("&c=").Append(Uri.EscapeDataString(Config)); 93 | if (!string.IsNullOrEmpty(Key)) 94 | { 95 | builder.Append("&key=").Append(Uri.EscapeDataString(Key)); 96 | } 97 | if (!string.IsNullOrEmpty(Version)) 98 | { 99 | builder.Append("&v=").Append(Uri.EscapeDataString(Version)); 100 | } 101 | 102 | return $"{Scheme}://{Host}:{Port}/chart?{builder}"; 103 | } 104 | 105 | public string GetShortUrl() 106 | { 107 | if (Config == null) 108 | { 109 | throw new NullReferenceException("You must set Config on the QuickChart object before generating a URL"); 110 | } 111 | 112 | JsonSerializerOptions options = new JsonSerializerOptions(); 113 | options.IgnoreNullValues = true; 114 | 115 | String json = JsonSerializer.Serialize(new 116 | { 117 | width = Width, 118 | height = Height, 119 | backgroundColor = BackgroundColor, 120 | devicePixelRatio = DevicePixelRatio, 121 | format = Format, 122 | chart = Config, 123 | key = Key, 124 | version = Version, 125 | }, options); 126 | 127 | string url = $"{Scheme}://{Host}:{Port}/chart/create"; 128 | 129 | HttpResponseMessage response = Client.PostAsync( 130 | url, 131 | new StringContent(json, Encoding.UTF8, "application/json") 132 | ).Result; 133 | 134 | if (!response.IsSuccessStatusCode) 135 | { 136 | throw new ApiException("Unsuccessful response from API", response); 137 | } 138 | 139 | string responseText = response.Content.ReadAsStringAsync().Result; 140 | QuickChartShortUrlResponse result = JsonSerializer.Deserialize(responseText); 141 | return result.url; 142 | } 143 | 144 | public byte[] ToByteArray() 145 | { 146 | if (Config == null) 147 | { 148 | throw new NullReferenceException("You must set Config on the QuickChart object before generating a URL"); 149 | } 150 | 151 | JsonSerializerOptions options = new JsonSerializerOptions(); 152 | options.IgnoreNullValues = true; 153 | 154 | String json = JsonSerializer.Serialize(new 155 | { 156 | width = Width, 157 | height = Height, 158 | backgroundColor = BackgroundColor, 159 | devicePixelRatio = DevicePixelRatio, 160 | format = Format, 161 | chart = Config, 162 | key = Key, 163 | version = Version, 164 | }, options); 165 | 166 | string url = $"{Scheme}://{Host}:{Port}/chart"; 167 | 168 | HttpResponseMessage response = Client.PostAsync( 169 | url, 170 | new StringContent(json, Encoding.UTF8, "application/json") 171 | ).Result; 172 | 173 | if (!response.IsSuccessStatusCode) 174 | { 175 | throw new ApiException("Unsuccessful response from API", response); 176 | } 177 | 178 | return response.Content.ReadAsByteArrayAsync().Result; 179 | } 180 | 181 | public void ToFile(string filePath) 182 | { 183 | File.WriteAllBytes(filePath, this.ToByteArray()); 184 | } 185 | } 186 | 187 | public class ApiException : Exception 188 | { 189 | public ApiException(string message, HttpResponseMessage response) : base(message) 190 | => this.Response = response; 191 | 192 | public HttpResponseMessage Response { get; private set; } 193 | } 194 | } 195 | -------------------------------------------------------------------------------- /QuickChart/QuickChart.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Library 5 | netcoreapp3.1;netstandard2.0;net472 6 | true 7 | Ian Webster 8 | Ian Webster 9 | MIT 10 | QuickChart 11 | https://github.com/typpo/quickchart-csharp 12 | Client library for the QuickChart Chart API, used to generate chart images. 13 | chart image, chart api, chart, charts, image charts 14 | QuickChart 15 | Client library for the QuickChart Chart API, used to generate chart images. 16 | 2.3.0 17 | QuickChart 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /QuickChartExamples/QuickChartExamples.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | netcoreapp3.1 6 | QuickChartExamples.SimpleExample 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /QuickChartExamples/ShortUrl.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using QuickChart; 3 | 4 | namespace QuickChartExamples 5 | { 6 | public class ShortUrlExample 7 | { 8 | static void Main(string[] args) { 9 | Console.WriteLine("Fetching short url..."); 10 | Chart qc = new Chart(); 11 | 12 | qc.Width = 500; 13 | qc.Height = 300; 14 | qc.Config = @"{ 15 | type: 'bar', 16 | data: { 17 | labels: ['Q1', 'Q2', 'Q3', 'Q4'], 18 | datasets: [{ 19 | label: 'Users', 20 | data: [50, 60, 70, 180] 21 | }] 22 | } 23 | }"; 24 | 25 | Console.WriteLine(qc.GetShortUrl()); 26 | } 27 | } 28 | } -------------------------------------------------------------------------------- /QuickChartExamples/Simple.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using QuickChart; 3 | 4 | namespace QuickChartExamples 5 | { 6 | public class SimpleExample 7 | { 8 | static void Main(string[] args) { 9 | Console.WriteLine("Writing simple URL..."); 10 | Chart qc = new Chart(); 11 | 12 | qc.Width = 500; 13 | qc.Height = 300; 14 | qc.Config = @"{ 15 | type: 'bar', 16 | data: { 17 | labels: ['Q1', 'Q2', 'Q3', 'Q4'], 18 | datasets: [{ 19 | label: 'Users', 20 | data: [50, 60, 70, 180] 21 | }] 22 | } 23 | }"; 24 | 25 | Console.WriteLine(qc.GetUrl()); 26 | } 27 | } 28 | } -------------------------------------------------------------------------------- /QuickChartExamples/ToFile.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using QuickChart; 3 | 4 | namespace QuickChartExamples 5 | { 6 | public class ToFileExample 7 | { 8 | static void Main(string[] args) { 9 | Console.WriteLine("Saving to disk..."); 10 | Chart qc = new Chart(); 11 | 12 | qc.Width = 500; 13 | qc.Height = 300; 14 | qc.Config = @"{ 15 | type: 'bar', 16 | data: { 17 | labels: ['Q1', 'Q2', 'Q3', 'Q4'], 18 | datasets: [{ 19 | label: 'Users', 20 | data: [50, 60, 70, 180] 21 | }] 22 | } 23 | }"; 24 | 25 | qc.ToFile("/tmp/chart.png"); 26 | } 27 | } 28 | } -------------------------------------------------------------------------------- /QuickChartTest/QuickChartTest.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using Xunit; 3 | 4 | using QuickChart; 5 | 6 | namespace QuickChartTest 7 | { 8 | public class QuickChartTest 9 | { 10 | [Fact] 11 | public void TestBasic() 12 | { 13 | Chart qc = new Chart 14 | { 15 | Width = 500, 16 | Height = 300, 17 | Config = @"{ 18 | type: 'bar', 19 | data: { 20 | labels: ['Q1', 'Q2', 'Q3', 'Q4'], 21 | datasets: [{ 22 | label: 'Users', 23 | data: [50, 60, 70, 180] 24 | }] 25 | } 26 | }" 27 | }; 28 | 29 | string url = qc.GetUrl(); 30 | Assert.Contains("https://quickchart.io:443/chart", url); 31 | Assert.Contains("w=500", url); 32 | Assert.Contains("h=300", url); 33 | Assert.DoesNotContain("key=", url); 34 | } 35 | 36 | [Fact] 37 | public void TestWithKey() 38 | { 39 | Chart qc = new Chart 40 | { 41 | Key = "abc123", 42 | Width = 500, 43 | Height = 300, 44 | Config = @"{ 45 | type: 'bar', 46 | data: { 47 | labels: ['Q1', 'Q2', 'Q3', 'Q4'], 48 | datasets: [{ 49 | label: 'Users', 50 | data: [50, 60, 70, 180] 51 | }] 52 | } 53 | }" 54 | }; 55 | 56 | string url = qc.GetUrl(); 57 | Assert.Contains("https://quickchart.io:443/chart", url); 58 | Assert.Contains("w=500", url); 59 | Assert.Contains("h=300", url); 60 | Assert.Contains("key=abc123", url); 61 | } 62 | 63 | [Fact] 64 | public void TestWithFormat() 65 | { 66 | Chart qc = new Chart 67 | { 68 | Width = 500, 69 | Height = 300, 70 | Format = "svg", 71 | Config = @"{ 72 | type: 'bar', 73 | data: { 74 | labels: ['Q1', 'Q2', 'Q3', 'Q4'], 75 | datasets: [{ 76 | label: 'Users', 77 | data: [50, 60, 70, 180] 78 | }] 79 | } 80 | }" 81 | }; 82 | 83 | string url = qc.GetUrl(); 84 | Assert.Contains("https://quickchart.io:443/chart", url); 85 | Assert.Contains("w=500", url); 86 | Assert.Contains("h=300", url); 87 | Assert.Contains("f=svg", url); 88 | } 89 | 90 | [Fact] 91 | public void TestWithVersion() 92 | { 93 | Chart qc = new Chart 94 | { 95 | Width = 500, 96 | Height = 300, 97 | Version = "2.9.4", 98 | Config = @"{ 99 | type: 'bar', 100 | data: { 101 | labels: ['Q1', 'Q2', 'Q3', 'Q4'], 102 | datasets: [{ 103 | label: 'Users', 104 | data: [50, 60, 70, 180] 105 | }] 106 | } 107 | }" 108 | }; 109 | 110 | string url = qc.GetUrl(); 111 | Assert.Contains("https://quickchart.io:443/chart", url); 112 | Assert.Contains("w=500", url); 113 | Assert.Contains("h=300", url); 114 | Assert.Contains("v=2.9.4", url); 115 | } 116 | 117 | [Fact] 118 | public void TestWithSelfHostingQuickChart() 119 | { 120 | var scheme = "http"; 121 | var host = "localhost"; 122 | var port = 47000; 123 | Chart qc = new Chart(scheme, host, port); 124 | qc.Width = 500; 125 | qc.Height = 300; 126 | qc.Config = @"{ 127 | type: 'bar', 128 | data: { 129 | labels: ['Q1', 'Q2', 'Q3', 'Q4'], 130 | datasets: [{ 131 | label: 'Users', 132 | data: [50, 60, 70, 180] 133 | }] 134 | }[] 135 | }"; 136 | 137 | string url = qc.GetUrl(); 138 | Assert.Contains($"{scheme}://{host}:{port}/chart", url); 139 | Assert.Contains("w=500", url); 140 | Assert.Contains("h=300", url); 141 | Assert.DoesNotContain("key=", url); 142 | } 143 | } 144 | } 145 | -------------------------------------------------------------------------------- /QuickChartTest/QuickChartTest.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | netcoreapp3.1 5 | 6 | false 7 | 8 | 9 | 10 | 11 | 12 | runtime; build; native; contentfiles; analyzers; buildtransitive 13 | all 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # quickchart-csharp 2 | 3 | [![Build Status](https://travis-ci.com/typpo/quickchart-csharp.svg?branch=main)](https://travis-ci.com/typpo/quickchart-csharp) 4 | [![Nuget](http://img.shields.io/nuget/v/QuickChart.svg?style=flat)](https://www.nuget.org/packages/QuickChart) 5 | 6 | A C# client for the [quickchart.io](https://quickchart.io/) chart API. 7 | 8 | ## Installation 9 | 10 | Use `QuickChart/QuickChart.cs` in this project, or install the `QuickChart` package from [NuGet](https://www.nuget.org/packages/QuickChart): 11 | 12 | ``` 13 | PM> Install-Package QuickChart -Version 2.1.0 14 | ``` 15 | or 16 | ``` 17 | dotnet add package QuickChart --version 2.1.0 18 | ``` 19 | --- 20 | ## Usage 21 | 22 | This library provides a `QuickChart` namespace containing a `Chart` class. Import and instantiate it. Then set properties on it and specify a [Chart.js](https://chartjs.org) config: 23 | 24 | ```csharp 25 | Chart qc = new Chart(); 26 | 27 | qc.Width = 500; 28 | qc.Height = 300; 29 | qc.Version = "2.9.4"; 30 | qc.Config = @"{ 31 | type: 'bar', 32 | data: { 33 | labels: ['Q1', 'Q2', 'Q3', 'Q4'], 34 | datasets: [{ 35 | label: 'Users', 36 | data: [50, 60, 70, 180] 37 | }] 38 | } 39 | }"; 40 | ``` 41 | 42 | Use `GetUrl()` on your QuickChart object to get the encoded URL that renders your chart: 43 | 44 | ```csharp 45 | Console.WriteLine(qc.GetUrl()); 46 | // https://quickchart.io/chart?c=%7B%22chart%22%3A+%7B%22type%22%3A+%22bar%22%2C+%22data%22%3A+%7B%22labels%22%3A+%5B%22Hello+world%22%2C+%22Test%22%5D%2C+%22datasets%22%3A+%5B%7B%22label%22%3A+%22Foo%22%2C+%22data%22%3A+%5B1%2C+2%5D%7D%5D%7D%7D%7D&w=600&h=300&bkg=%23ffffff&devicePixelRatio=2.0&f=png 47 | ``` 48 | 49 | If you have a long or complicated chart, use `GetShortUrl()` to get a fixed-length URL using the quickchart.io web service (note that these URLs only persist for a short time unless you have a subscription): 50 | 51 | ```csharp 52 | Console.WriteLine(qc.GetShortUrl()); 53 | // https://quickchart.io/chart/render/f-a1d3e804-dfea-442c-88b0-9801b9808401 54 | ``` 55 | 56 | The URLs will render an image of a chart: 57 | 58 | 59 | 60 | --- 61 | 62 | ### Customizing your chart 63 | 64 | You can set the following properties: 65 | 66 | #### Config: string 67 | The actual Chart.js chart configuration. 68 | 69 | #### Width: int 70 | Width of the chart image in pixels. Defaults to 500 71 | 72 | #### Height: int 73 | Height of the chart image in pixels. Defaults to 300 74 | 75 | #### BackgroundColor: string 76 | The background color of the chart. Any valid HTML color works. Defaults to #ffffff (white). Also takes rgb, rgba, and hsl values. 77 | 78 | #### DevicePixelRatio: double 79 | 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. 80 | 81 | #### Format: string 82 | The output format of the chart. Defaults to "png" 83 | 84 | #### Version: string 85 | Chart.js version. See [documentation](https://quickchart.io/documentation/#parameters) for supported versions. 86 | 87 | #### Key: string 88 | API key (not required) 89 | 90 | --- 91 | 92 | ### Creating chart URLs 93 | 94 | There are a few ways to get a URL for your chart object. 95 | 96 | #### GetUrl(): string 97 | 98 | Returns a URL that will display the chart image when loaded. 99 | 100 | #### GetShortUrl(): string 101 | 102 | Uses the quickchart.io web service to create a fixed-length chart URL that displays the chart image. Returns a URL such as `https://quickchart.io/chart/render/f-a1d3e804-dfea-442c-88b0-9801b9808401`. 103 | 104 | Note that short URLs expire after a few days for users of the free service. You can [subscribe](https://quickchart.io/pricing/) to keep them around longer. 105 | 106 | --- 107 | 108 | #### Other methods 109 | 110 | #### ToFile(string path) 111 | 112 | Write your chart to file. 113 | 114 | #### ToByteArray(): byte[] 115 | 116 | Returns an array of bytes representing your image. 117 | 118 | ## More examples 119 | 120 | Checkout the `QuickChartExamples` project to see other usage. 121 | --------------------------------------------------------------------------------