This repository has been archived by the owner on Aug 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #51 from azavea/feature/dpd/add-export-services
Add Export Services from Climate Change Lab
- Loading branch information
Showing
8 changed files
with
87 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,7 @@ | |
*.launch | ||
.settings/ | ||
*.sublime-workspace | ||
.*.swp | ||
|
||
# IDE - VSCode | ||
.vscode/* | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
export { ChartsModule } from './charts.module'; | ||
export { DataExportService } from './services/data-export.service'; | ||
export { ImageExportService } from './services/image-export.service'; | ||
|
||
export { ModelModalComponent } from './components/model-modal/model-modal.component'; |
15 changes: 15 additions & 0 deletions
15
src/lib/modules/charts/services/data-export.service.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { DataExportService } from './data-export.service'; | ||
|
||
|
||
describe('DataExportService', () => { | ||
|
||
let service: DataExportService; | ||
|
||
beforeEach(() => { | ||
service = new DataExportService(); | ||
}); | ||
|
||
it('should have a downloadAsJSON method', () => { | ||
expect(service.downloadAsJSON).toBeDefined(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { Injectable } from '@angular/core'; | ||
import * as FileSaver from 'file-saver'; | ||
|
||
/* | ||
* Generates JSON from data | ||
*/ | ||
@Injectable() | ||
export class DataExportService { | ||
|
||
public downloadAsJSON(filename: string, data: any): void { | ||
this.downloadFile(JSON.stringify(data), filename, 'application/json'); | ||
} | ||
|
||
private downloadFile(data: string, title: string, mime: string): void { | ||
FileSaver.saveAs(new File([data], title, {type: mime + ';charset=utf-8'})); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { Injectable } from '@angular/core'; | ||
import * as SaveSvg from 'save-svg-as-png'; | ||
|
||
/* | ||
* Generates image of D3 chart SVG for download | ||
*/ | ||
@Injectable() | ||
export class ImageExportService { | ||
|
||
/** | ||
* Options to pass when converting SVG to PNG | ||
* | ||
* @param parentSelector - Name of parent of chart, used to rewrite CSS selectors | ||
*/ | ||
private chartOptions(parentSelector: string) { | ||
return { | ||
backgroundColor: 'white', | ||
selectorRemap: function(selector) { | ||
// find CSS selectors mapped to parent chart | ||
return selector.replace(parentSelector, ''); | ||
} | ||
}; | ||
} | ||
|
||
/** | ||
* Converts chart SVG to PNG and downloads it. | ||
* | ||
* @param indicatorName - Name of indicator, used for SVG selector | ||
* @param fileName - File name for download, will be suffixed with extension | ||
*/ | ||
public downloadAsPNG(indicatorName: string, fileName: string, selector: string): void { | ||
const filename: string = fileName + '.png'; | ||
const svg: HTMLElement = document.getElementById('chart-' + indicatorName); | ||
// SVG might not be found if chart hasn't loaded yet | ||
if (!svg) { return; } | ||
|
||
SaveSvg.saveSvgAsPng(svg, filename, this.chartOptions(selector)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2922,6 +2922,11 @@ file-loader@^1.1.5: | |
loader-utils "^1.0.2" | ||
schema-utils "^0.3.0" | ||
|
||
file-saver@^1.3.8: | ||
version "1.3.8" | ||
resolved "https://registry.yarnpkg.com/file-saver/-/file-saver-1.3.8.tgz#e68a30c7cb044e2fb362b428469feb291c2e09d8" | ||
integrity sha512-spKHSBQIxxS81N/O21WmuXA2F6wppUCsutpzenOeZzOCCJ5gEfcbqJP983IrpLXzYmXnMUa6J03SubcNPdKrlg== | ||
|
||
filename-regex@^2.0.0: | ||
version "2.0.1" | ||
resolved "https://registry.yarnpkg.com/filename-regex/-/filename-regex-2.0.1.tgz#c1c4b9bee3e09725ddb106b75c1e301fe2f18b26" | ||
|
@@ -6990,6 +6995,11 @@ saucelabs@~1.3.0: | |
dependencies: | ||
https-proxy-agent "^1.0.0" | ||
|
||
save-svg-as-png@^1.4.12: | ||
version "1.4.12" | ||
resolved "https://registry.yarnpkg.com/save-svg-as-png/-/save-svg-as-png-1.4.12.tgz#6acaa3b542d0236d6e96ff38c1da40a6b18bc15e" | ||
integrity sha512-XK3uMu53thxds/YhdBBGClySbmeDY8txEYzOCJBUi1Ahn3U+82OyoUk8E7Plfy6GXzVjKR2EPT6EDSCAY27gew== | ||
|
||
[email protected]: | ||
version "0.5.8" | ||
resolved "https://registry.yarnpkg.com/sax/-/sax-0.5.8.tgz#d472db228eb331c2506b0e8c15524adb939d12c1" | ||
|