Skip to content
This repository has been archived by the owner on Aug 21, 2023. It is now read-only.

Add Export Services from Climate Change Lab #51

Merged
merged 3 commits into from
Apr 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*.launch
.settings/
*.sublime-workspace
.*.swp

# IDE - VSCode
.vscode/*
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [UNRELEASED]
- Added `DataExportService` and `ImageDownloadService` to enable graph image downloads from Temperate.

## [0.3.1]
- Added new `...IndicatorDistanceQueryParams` types for providing an optional `distance: number` parameter for Lat/Lon climate data queries.
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,11 @@
},
"dependencies": {
"d3": "^4.10.0",
"file-saver": "^1.3.8",
"jquery": "^3.2.1",
"lodash": "^4.17.4",
"moment": "^2.18.1",
"save-svg-as-png": "^1.4.12",
"@types/geojson": "^1.0.3"
},
"devDependencies": {
Expand Down
2 changes: 2 additions & 0 deletions src/lib/modules/charts/index.ts
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 src/lib/modules/charts/services/data-export.service.spec.ts
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();
});
});
17 changes: 17 additions & 0 deletions src/lib/modules/charts/services/data-export.service.ts
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'}));
}
}
39 changes: 39 additions & 0 deletions src/lib/modules/charts/services/image-export.service.ts
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) {
Copy link
Contributor Author

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.

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));
}
}
10 changes: 10 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down