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
Add Export Services from Climate Change Lab #51
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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" | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This used to be an object; apologies for including the switch to a function in the same commit that brings it over into the repo. There was a lot of friction to getting the Components repo linked in to both Temperate and the Lab, and by the time I got everything working, the WiP commits weren't in a place where it was super easy to split that functionality change apart. I'm happy to dive in and break this apart if you want to keep the commit history clean.