Last active
October 25, 2019 14:45
-
-
Save branflake2267/4652a5d7188dfe0b33d3d02a808d8d74 to your computer and use it in GitHub Desktop.
Example using ExtWebComponent Area Chart monthly stock data from AlphaVantage https://www.alphavantage.co
This file contains hidden or 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
| <ext-cartesian | |
| width="1000px" | |
| height="600px" | |
| downloadServerUrl="http://svg.sencha.io" | |
| shadow="true" | |
| insetPadding="25 35 0 10" | |
| axes='[{ | |
| "type": "numeric" , | |
| "position": "left" , | |
| "fields": [ "1. open" ], | |
| "label": { "rotate": { "degrees": "-30" } }, | |
| "grid": { "odd": { "fill": "#e8e8e8" } }, | |
| "title": { "text": "Alphabet Inc Stock Data" , "fontSize": "20" } | |
| }, | |
| { | |
| "type": "category", | |
| "position": "bottom", | |
| "fields": "time", | |
| "grid": "true", | |
| "title": { "text": "Monthly", "fontSize": "20" } | |
| }]' | |
| legend='{ | |
| "type": "sprite", | |
| "position": "bottom" | |
| }' | |
| series='[{ | |
| "type": "area" , | |
| "xField": "time", | |
| "yField": [ "1. open", "2. high", "3. low", "4. close" ], | |
| "title": [ "open", "high", "low", "close" ], | |
| "style": { "stroke": "black" , "lineWidth": "2", "fillOpacity": "0.8" }, | |
| "colors": ["#003f5c", "#58508d", "#bc5090", "#ff6361", "#ffa600"] | |
| }]' | |
| platformConfig='{ | |
| "phone": { "insetPadding": "15 5 0 0" } | |
| }'> | |
| </ext-cartesian> |
This file contains hidden or 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
| import template from './AreaChartComponent.html' | |
| Ext.require([ | |
| 'Ext.chart.theme.Midnight', | |
| 'Ext.chart.theme.Green', | |
| 'Ext.chart.theme.Muted', | |
| 'Ext.chart.theme.Purple', | |
| 'Ext.chart.theme.Sky', | |
| 'Ext.chart.series.Area', | |
| 'Ext.chart.axis.Numeric', | |
| 'Ext.chart.axis.Category' | |
| ]); | |
| class AreaChartComponent extends HTMLElement { | |
| constructor() { | |
| super() | |
| } | |
| connectedCallback() { | |
| this.innerHTML = template; | |
| this._fetchChartData(); | |
| } | |
| disconnectedCallback() { | |
| } | |
| attributeChangedCallback(attrName, oldVal, newVal) { | |
| } | |
| /** | |
| * Fetch the chart data from https://www.alphavantage.co/ using an API Key. | |
| * | |
| * TODO Fetch your api key here: https://www.alphavantage.co/support/#api-key | |
| */ | |
| _fetchChartData() { | |
| let me = this; | |
| let apiKey = 'demo'; | |
| let stockSymbol = 'GOOGL'; | |
| let url = `https://www.alphavantage.co/query?function=TIME_SERIES_MONTHLY&symbol=${stockSymbol}&apikey=${apiKey}`; | |
| fetch(url) | |
| .then(response => { | |
| return response.json(); | |
| }) | |
| .then(json => { | |
| return me._flattenData(json); | |
| }) | |
| .then(jsonflatRows => { | |
| me._renderChart(jsonflatRows); | |
| }) | |
| .catch(err => { | |
| console.log("error", err); | |
| }) | |
| } | |
| /** | |
| * The goal is to flatten the nested json data, so it's easy to consume in the charts. | |
| * @param json data | |
| * @returns {*[]} array of json data | |
| * @private | |
| */ | |
| _flattenData(json) { | |
| console.log("json=", json); | |
| let jsonTimes = json['Monthly Time Series'] | |
| let flatRows = []; | |
| for (let jsonTime in jsonTimes) { | |
| let row = { | |
| "time": jsonTime | |
| }; | |
| let jsonNestedTime = jsonTimes[jsonTime]; | |
| for (let nestedKey in jsonNestedTime) { | |
| row[nestedKey] = jsonNestedTime[nestedKey]; | |
| } | |
| flatRows.push(row); | |
| } | |
| return flatRows.reverse(); | |
| } | |
| _renderChart(jsonflatRows) { | |
| console.log('_renderChart jsonflatRows=', jsonflatRows); | |
| let store = Ext.create('Ext.data.Store', { | |
| fields: ["time", "1. open", "2. high", "3. low", "4. close", "5. volume"] | |
| }); | |
| store.loadData(jsonflatRows); | |
| let areaChartEl = this.querySelector('ext-cartesian'); | |
| areaChartEl.ext.bindStore(store); | |
| } | |
| } | |
| window.customElements.define('my-chart-area', AreaChartComponent); |
This file contains hidden or 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
| <my-chart-area></my-chart-area> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment

GOOGL Example

APPL Example
