aoPlotlyAddOn.newTimeseriesPlot( divInfo, data, otherDataProperties, dataSources, settings, timeInfo, layout, options );
This functions allows reading of data from various sources, processing of the data, calculation of traces and adds functionality to time series plots in Plotly.
It helps you in two stages: data sourcing and plot funtionality.
In addition, there are some utility functions to accomodate for particular cases:
aoPlotlyAddOn.readSomeDataSourcesIntoData( yourDataArray, otherDataProperties, dataSources, settings, timeInfo, myCallback );
This function will read data from a number of sources and save the processed data into yourDataArray in a ploltly style. This data can then be used to feed multiple instances of aoPlotlyAddOn.newTimeseriesPlot.
aoPlotlyAddOn.arrayCopy( array );
Returns a new array of values (shallow copy) which is a copy of the provided array. Use together with aoPlotlyAddOn.readSomeDataSourcesIntoData when assigning the readData to a new data for an instance of aoPlotlyAddOn.newTimeseriesPlot.
aoPlotlyAddOn.updateKnowRecessions( url );
This function will update the library recessions array with new periods, using the FRED api. This can be used when creating multiple plots in a page, so that the api call is made only once.
aoPlotlyAddOn.findDateFromTodayAsString( currentDateAsDate, requestCode, shiftNumber );
This utility function helps with some date calculations. Use it to feed the timeInfo object.
Data sources: handle various data sources with preprocessing options. For example, include url to a csv file, provide options to sort, or change dates formats
Plot functionality
The divInfo object contains the following properties:
wholeDivID (string) "your whole div id". Whole div name, where you will have your plot, including other html items, like your titles and footnotes. Required to hide the div while the plot loads.
plotDivID "(string) "your plotly Div id". Div where the plot will be included. It should be a div within wholeDiv. Before plotDiv you would place the plot title and other header information, and after plotDiv you would add footnotes.
noLoadedDataMessage "(string) A message to be displayed in case there was no data to be plotted. .
onErrorHideWholeDiv "(boolean) Default: false. Set to true if you want wholeDiv hidden in case there was no data to be plotted. The noLoadedDataMessage will be displayed. If set to false, the noLoadedDataMessage will be displayed in the plotDiv.
In your html you would structure the wholeDiv and plotDiv and include your plot titles and footnotes in between:
<body>
<div id="myWholeDiv_01" style="visibility:hidden">
<!-- Include any header html items to your plot -->
<h3>My Plot TiTle</h3>
<div id="myPlotDiv_01" class="plotly" align="left" style="width:100%; height:480px;"></div>
<!-- Include any footer html items to your plot -->
(*) Shaded areas indicate recessions.<br>
Data source: <a href="https://www.quandl.com">Quandl.</a>
</div>
<script>
<!-- JAVASCRIPT CODE GOES HERE -->
</script>
</body>
In your javascript, declare and defined the divInfo object:
var divInfo = {
//whole div, including your titles and footnotes.
// required to hide div while plot loads.
wholeDivID: "myWholeDiv_01",
// div in which plot will be included
// should be a div within wholeDiv
plotDivID: "myPlotDiv_01"
};
Data follows Plotly's structure and properties. Data is an array of objects, each one with the information for each trace in the plot. The properties x and y, with the dates and value arrays may be supplied directly or be added by the function based on the data sources parameters.
Data example with three traces displayed and one used to make calculations as deflactor. One of the traces has values defined, the others will be fed using the function and the dataSources parameters.
in your javascript:
var data = [
{
type: "scatter",
name: "S&P 500",
mode: "lines",
opacity: 1,
// the area below this trace will be filled
// include a transparency percentage so as not to hide the traces there
fill: "tozeroy",
fillcolor: "rgba(205, 245,255, 0.20)", //#FAFCFD + 50% transparency
line: {
color: "#1A5488",
width: 3,
dash: "solid"
}
},{
x: ["2001-01-31", "2017-05-12"];
y: [ 150, 200];
type: "scatter",
name: "my trace",
mode: "lines",
opacity: 1,
line: {
color: "#1A5488",
width: 3,
dash: "solid"
}
}, {
type: "scatter",
name: "US CPI",
mode: "lines",
opacity: 0.8,
line: {
color: "#000000",
width: 1,
dash: "dash"
}
},{
// this trace will not be displayed but be used as deflactor,
// assuming real/nominal options in place
type: "scatter",
visible: false,
name: "US CPI",
mode: "lines",
line: {
color: "#000000",
width: 1,
dash: "dash"
}
}
];
This array of objects links the data array with the dataSources array using a common traceID. The traceID is independent from the trace name property, which is used by Plotly to name the trace.
The otherDataProperties array has the same number of elements as the data array.
It also includes other options for the traces, not part of plotly's traces properties.
Each object within the otherDataProperties array has the the following properties:
calculate: (object, optional) include this object to set calculaiton options for this trace. Instead of loading data, the trace dates and value will be calculated base on other traces and parameters. There are two options for the calculate object:
Calculating a real trace (deflated values)
Calculating a trace using a custom function
in your javascript:
var otherDataProperties = [
{
traceID: "S&P 500",
// this trace will be recalculated if a nominal to real change of base is applied
toggleRealNominal: true,
},
{
traceID: "US CPI"
toggleRealNominal: true,
},{
// this trace is defined as the deflactor,
// it is not changed when a nominal / real change of base is applied to the traces
traceID: "US CPI deflactor"
deflactor: true,
toggleRealNominal: false,
}
];
This is and array of objects. It has as many elements as sources of data you may have.
Each object in the dataSouces will get a chunk of data, process it and feed as many traces as instructed.
url (string) Full url where data is to be read from. Required except for "arrayOfJsons" urlType.
urlType (string) Any of "csv", "EiaJson", "yqlJson", "yqlGoogleCSV", "pureJson" or "arrayOfJsons".
csv: A csv file will be read from the url using Plotly.d3.csv.
yqlJson: use this option if data is sourced using yql and is returned as json. From readJson returned by $.getJSON, json object would be readJson.query.results.json.observation.
An example:
get json data from FRED through yql:
var fredKey = "your FRED key";
var seriesId = "UNRATE";
var seriesUnits ="lin";
var urlFred = "https://api.stlouisfed.org/fred/series/observations?"+
"series_id="+seriesId+"&api_key="+fredKey+"&units="+seriesUnits+"&file_type=json";
var baseUri = "https://query.yahooapis.com/v1/public/yql?q=";
var uriQuery = encodeURIComponent("SELECT * from json where url='"+urlFred+"'");
var url = baseUri + uriQuery+"&format=json";
EiaJson: data will be read from the Energy Information Admisnitration api. In this case, you will need to provide a seriesIndex for each of the traces read from this source (see traces array below), ( dataSources.traces.seriesIndex) starting from 0 in the order the api returns the data.
An example:
url to get data from EIA's api (will retrieve 4 series):
var eiaKey = "your EIA key";
var rootEIA = "https://api.eia.gov/series/?series_id=";
var eiaSuffix = "&out=json";
var url = rootEIA +
"TOTAL.PAPSPOC.M;TOTAL.PAPSPUS.M;TOTAL.PAPSPEU.M;TOTAL.PAPSPJA.M" +
"&api_key="+eiaKey+eiaSuffix;
/*
* In this examples, the series should be referred to as seriesIndex: 0, seriesIndex: 1,... etc.
* in the traces array in the same order in which the traces are set in the url
*
*/
var dataSources = [{
urlType: "EiaJson",
url: url,
traces: [{
traceID: "myTraceId1",
seriesIndex: 0
},...
{
traceID: "myTraceId4",
seriesIndex: 3
}]
}];
WBJson: data will be read from the World Bank api. In this case, you will need to provide a seriesIndex for each of the traces read from this source ( dataSources.traces.seriesIndex) starting from 0 in the order the api returns the data. Up to this date, the World Bank api doesn't allow for multiple series, but in case it is changed, you would provided a seriesIndex for each of the traces read from this source (see traces array below). The parameter format=json is required. Provide also a high enough value for per_page parameter, so the the api returns only one page of data (the function currently loads the data from the first page returned.
An example:
url to get data from the World Bank api (retrieves one serie):
var url = "https://api.worldbank.org/v2/countries/WLD/indicators/GOLD?format=json&per_page=9000";
/*
* In this examples, the series should be referred to as seriesIndex: 0, seriesIndex: 1,... etc.
* in the traces array in the same order in which the traces are set in the url
*
*/
var dataSources = [{
urlType: "WBJson",
url: url,
traces: [{
traceID: "Monthly_Historical_Nominal",
seriesIndex: 0
}]
}];
fredJson: data will be read from the FRED api. Only on serie can be read from each url (dataSource). If you need more than one serie from the FRED, place as many items in the dataSources arra. You need to use xSeriesName: "date", xSeriesName: "value", postProcessDate:"end of month" and firstItemToRead: "last".
An example:
url to get data from FRED's api (retrieves 1 series):
var SeriesId =
var url = "https://api.stlouisfed.org/fred/series/observations?series_id=" +
"GNPCA&api_key=PUTYOURFREDAPIKEY&fyle_type=json"
var dataSources = [{
urlType: "fredJson",
url: url,
traces: [{
traceID: "Monthly_Historical_Nominal",
xSeriesName: "date",
ySeriesName: "value",
postProcessDate: "end of month", // optional, FRED sets dates on the first day of period, use it to
// transform dates to end of period
firstItemToRead: "last" //required because FRED returns data in chronological order
}]
}];
yqlGoogleCSV: In this case, the url to be provided is a google url that returns a csv file. The yql portion will be added by the function as "https://query.yahooapis.com/v1/public/yql?q="+encodeURIComponent("SELECT * from csv where url='"+url+"'")+"&format=json"
. From readJson returned by $.getJSON
(or Plotly.d3.json
), json object would be readJson.query.results.row
.
pureJson: Use this case when you provide and url that returns an array of jsons. The url will be processed with $.getJSON
(or Plotly.d3.json
). An array of jons will have one object for each data point. Each object should contain at least a property for the dates vales and a property for the y value. This arrayOfJsons has the same structure as that returned by Plotly.d3.csv.
arrayOfJsons: Use this case to provide data you sourced from elsewhere, that you would like to be processed (change of date format, or calculate adjusted values). An array of jons will have one object for each data point. Each object should contain at least a property for the dates vales and a property for the y value. This arrayOfJsons has the same structure as that returned by Plotly.d3.csv. In this case, the function will not get data from an url.
xSeriesName: (string) Label that identifies dates as they appear in the CSV or Json files. You may place it here, if all traces from this source have the same xSeriesName, or in the traces array.
ySeriesName: (string) Label that identifies y values as they appear in the CSV or Json files. You may place it here, if all traces from this source have the same ySeriesName, or in the traces array.
Example:
your csv file:
date,open,high,low,close
2000-12-31,14,16,13,15
1999-12-31,15,17,13,16
or your array of jsons
[{date: "2000-12-31", open: 14, high: 16, low: 13, close: 15},
{date: "1999-12-31", open: 15, high: 17, low: 13, close: 16}]
you want:
data = [
{x: ["2000-12-31", "1999-12-31"],
y: [ 15, 16]},
{x: ["2000-12-31", "1999-12-31"],
y: [ 16, 17]}
];
so you would set in dataSources:
dataSources[0].xSeriesName = "date";
dataSources[0].trace[0].ySeriesName = "close";
dataSources[0].trace[1].ySeriesName = "high";
xDateSuffix: (string) Optional. To add information to read Dates. Could have content like " 00:00:00-04:00", to provide time and time zone offset. You may place it here, if all traces from this source have the same XDateSuffix, or in the traces array.
onlyAddDateSuffix: (string) Optional. If provided, dates will be processed by only adding this suffix. You may place it here, if all traces from this source have the same option, or in the traces array.
processDates: (boolean) Optional. If set to false, no processing of dates will be made (no adding of suffixes or change to end of month or any other). Set to false only if dates are consistent all over the data arrays and have "yyyy-dd-mm 00:00:00-04:00" format which includes time and timezone offset. Otherwise, dates may produce undesired results as browsers translate then to local timezones and may assume dates "yyyy-mm-dd" where GMT. You may place it here, if all traces from this source have the same option, or in the traces array.
postProcessData: "end of month" Optional. Could be used for series where dates are provided at beginning of month, and need to be converted to end of month. You may place it here, if all traces from this source have the same postProcessDate, or in the traces array.
calculateAdjustedClose: (boolean) Optional. If set to true, traces that come from more than one source will be normalized using the overlapping date. Older values will be changed. You need to provide at least one overlapping date in order for this option to be applied. You may place it here, if all traces from this source have the same option, or in the traces array.
sort: (boolean) Optional. If set to true, all values as ySeriesNames in use with this xSeriesName and this xSeriesName will be sorted. This function works with dates ordered from latest to oldest. You may place it here, if all traces from this source have the same option, or in the traces array.
traces (array of objects) one object for each trace to be fed with a source
dataSources[i].traces[j].tracesID === otherDataParameters[k].tracesID -> data goes into data[k].x and data[k].y
Example 1:
var dataSources = [ { // urlType could be "csv", urlType: "csv",
url: "https://rawgit.com/ajoposor/test-csv-files/files/SP500%20sectors-1998-12-2017-04.csv",
// preprocessing options could be added here or in one or more objects of the traces array below
onlyAddXDateSuffix: " 00:00:00-04:00",
traces: [
{
xSeriesName: "Date",
ySeriesName: "SP500 Adjusted Close",
traceID: "S&P 500"
},{
xSeriesName: "Date_CPI",
ySeriesName: "CPI_EOM",
traceID: "US CPI"
}, {
xSeriesName: "Date_CPI",
ySeriesName: "CPI_EOM",
traceID: "US CPI deflactor"
}
]
}
];
***Example 2**: Test sorting and dates processing options.*
```javascript
var dataSources = [
{
urlType: "arrayOfJsons",
// all options could be set at dataSources level or at traces level
// if set at dataSources lelvel they will override traces options.
// firstItemToRead: "last",
// processDates: true,
// optional sort: true / false,
// optional postProcessDate: "end of month",
// optional xSeriesName: in case used from postProcessDate or sort
// xDateSuffix: "",//"T00:00:00-04:00",
arrayOfJsons: [
{"Date":"2010-05-24","Date2":"1998-12-31","Open":"135.82","Close":"116.152","Adj Close":"115.173210"},
{"Date":"2011-03-04","Date2":"2010-05-24","Open":"110.87","Close":"116.017","Adj Close":"130.044304"},
{"Date":"2011-05-05","Date2":"2012-08-06","Open":"165.94","Close":"116.611","Adj Close":"145.629341"},
{"Date":"2012-08-06","Date2":"2011-05-05","Open":"155.79","Close":"117.914","Adj Close":"155.918411"},
{"Date":"2012-10-09","Date2":"2013-03-10","Open":"190.95","Close":"118.988","Adj Close":"175.989319"},
{"Date":"2013-03-10","Date2":"2012-10-09","Open":"185.77","Close":"119.111","Adj Close":"180.108315"},
{"Date":"2013-10-11","Date2":"2014-06-12","Open":"300.74","Close":"119.750","Adj Close":"200.742935"},
{"Date":"2014-06-12","Date2":"2013-10-11","Open":"250.90","Close":"119.250","Adj Close":"250.247139"},
{"Date":"2015-02-13","Date2":"2015-12-17","Open":"270.10","Close":"119.041","Adj Close":"240.038902"},
{"Date":"2015-12-17","Date2":"2015-02-13","Open":"290.38","Close":"120.000","Adj Close":"220.990829"},
{"Date":"2016-01-18","Date2":"2016-04-19","Open":"320.99","Close":"119.988","Adj Close":"240.980911"},
{"Date":"2016-04-19","Date2":"2016-01-18","Open":"280.39","Close":"119.779","Adj Close":"280.772675"},
{"Date":"2016-06-20","Date2":"2017-06-11","Open":"340.43","Close":"120.000","Adj Close":"300.990829"},
{"Date":"2017-01-23","Date2": "","Open": "","Close":"120.08","Adj Close":"280.070"},
{"Date":"2017-05-24","Date2": "","Open": "","Close":"119.97","Adj Close":"320.960"}
],
traces:[
{
// dates follow chronological order, they will be reordered with the firstItemToRead option
xSeriesName: "Date",
ySeriesName: "Adj Close",
traceID: "S&P 500",
// in case another source of data is used to feed this trace (traceID: "S&P 500")
// values will be adjusted using a common date
calculateAdjustedClose: true,
// dates must follow most recent to oldest order
firstItemToRead: "last",
},
{
xSeriesName: "Date",
ySeriesName: "Close",
traceID: "Some ID",
calculateAdjustedClose: true,
firstItemToRead: "last",
},
{
// this dates have no order, they will be sorted from recent to older with the sort option
// missing dates will not be passed to the data
xSeriesName: "Date2",
ySeriesName: "Open",
traceID: "Second Dates",
calculateAdjustedClose: true,
sort: true
}
]
}};
Example 3: Dates are to be changed to end of month.
var dataSources = [
{
urlType: "csv",
url: "https://www.quandl.com/api/v3/datasets/FRED/CPIAUCSL.csv?api_key=yourQuandlApiKey",
traces:[
{
xSeriesName: "Date",
ySeriesName: "Value",
postProcessDate: "end of month",
traceID: "US CPI"
},
{
xSeriesName: "Date",
ySeriesName: "Value",
postProcessDate: "end of month",
traceID: "US CPI deflactor"
}
]
}
];
This is an object that controls the features added to your plot.
allowFrequencyResampling: (boolean) Optional. This will add buttons and calculate traces for different frequencies (weekly, monthly, ..., yearly) and aggregations (close, average, change, percentage change, etc.)
desiredFrequencies: (array of strings) Include frequencies to be calculated (in case allowFrequencyResampling set to true) Available: "daily", "weekly", "monthly", "quarterly", "semiannual", "annual".
series: (object) To provide a name for the frequency and aggregation of the supplied data (initial):
baseFrequency: (string) To provide a name for the base frequencies as per initial data provided. Could be "base", "daily", "weekly", "monthly", "quarterly", "annual" or your custom name or none.
baseAggregation: (string) To provide a name of the base aggregation as per initial data provided. Could be "base", "close", "average", "change", "percChange", "sqrPercChange", "cumulative", your custom name or none.
targetFrequencyDisplay: (string) Minimum display granularity of x axis, could be "daily", "weekly", "monthly", "quarterly", "semiannual", "yearly".
endOfWeek: (integer between 0 and 6) Sets the end of week. Applies to calculation of weekly values and display of weeks. 0 Sunday, 1 Monday, etc.
displayRecessions: (boolean) Optional. Set to true to displays background area for recession periods.
recessionsFillColor: (string for color code) Optional. Default is "#000000". Set to your desired value. This will control the fillcolor of the shapes used to mark recession periods.
recessionsOpacity: (number) Optional. Default is 0.15. Set to your desired value. This will control the shapes used to mark recession periods.
queueConcurrencyLimit: (positive integer) Optional. Default is 10. Sets the value for maximum concurrent async tasks for reading external data from urls. ATTENTION: in case you feed a trace from many sources, set queueConcurrencyLimit to 1, so that the assembling of the trace is made synchronously, otherwise it may result in incorrect trace data values.
newRecessionsUrl: (string) Optional. Set to "" to avoid looking for new recessions dates. Recessions in library are current up to 2015-12-31. If you need to include new recessions enter an address that returns a zip file as provided by the FRED api for the serie_id USRECP.
The url for the zip file is: https://api.stlouisfed.org/fred/series/observations?series_id=USRECP&api_key=YourFredApiKey&file_type=txt&observation_start=2015-12-31
You may need to use a proxy to handle cross-origin HTTP requests. You may prefix the url with https://cors-anywhere.herokuapp.com/
which provides a proxy server.
allowRealNominal: (boolean) Optional. Set to true to displays button for convertion between real an nominal.
initialRealNominal: (string) Optional. Set to "real" or "nominal". Sets the initial display of traces to which real transformation is applicable)
baseRealDate: (string) Any of "end of range", "end of domain", "beggining of range", beggining of domain", or a date "yyyy-mm-dd hh:mm:ss.sss-04:00". It would set the base at which real values are to be calculated. Range refers to the displayed range and domain to the available date range for the deflactor trace.
allowDownload: (boolean) Optional. Would display button to download current displayed traces over its full dates domain.
downloadedFileName: (string) Optional. Name to be set to downloaded file.
xAxisNameOnCSV: (string) String to head dates on the downloaded csv file.
allowCompare: (boolean) displays button to allow comparison of traces to a base unit value.
transformToBaseIndex: (boolean) Optional, series would be transformed to common value of 1 at beginning of displayed dates range at the initial displayed plot.
allowSelectorOptions: (boolean) If set to true would display buttons for time range selection, 3m, 6m, 1y, YTD, 5y, etc.
allowLogLinear: (boolean) If set to true, display button to toogle yaxis to log/linear type.
textAndSpaceToTextRatio: (number) Default 1.8. Sets spacing of text to void space in xaxis ticks.
removeDoubleClickToZoomBackOut (boolean) Default: true. Use to allow or remove message displayed when range in plot area is selected. This message says Double-click to zoom back out.
waitForGlobalData (boolean) Default: false. Set to true if you are using aoPlotlyAddOn.readSomeDataSourcesIntoData to load reusable data, so that execution waits (at the appropriate point) for the external data being ready.
dataReadFlag (pointer to one element array containing true or false) Optional. In case you are using aoPlotlyAddOn.readSomeDataSourcesIntoData to load reusable data, set to the variable you passed this latter function to flag when the reusable data is loaded. The structure of the paramenter is myDataReadFlag (myDataReadFlag = [true/false])
. In case you are using aoPlotlyAddOn.readSomeDataSourcesIntoData this dataReadFlag property must be set to point to the array with the flag in both aoPlotlyAddOn.readSomeDataSourcesIntoData and each instance of aoPlotlyAddOn.newTimeseriesPlot where you will use the reusable data.
globalDataCallback (function) Optional. Provide a function to feed your data array x and y arrays with the reausable external data.
Example: in your javascript:
function loadDataCallback() {
data[0].x = aoPlotlyAddOn.arrayCopy(myReusableData[0].x);
data[0].y = aoPlotlyAddOn.arrayCopy(myReusableData[0].y);
}
locale (string) Optional. Specify the local code for the language to be used. The locale file should have been registered before. Default: true. Use to allow or remove message displayed when range in plot area is selected. This message says Double-click to zoom back out.
First, register the required language
<!-- register Brazilian Portuguese support for aoPlotlyAddOn.js -->
<!-- place this line after you have included aoPlotlyAddOn.js -->
<script src="https://rawgit.com/ajoposor/aoPlotlyAddOn/dist/aoPlotlyAddOn-locale-pt-br.js"></script>
Then, set the settings.locale to the language code, for example:
settings.locale = "pt-BR"
Use this object to instruct handling of dates range
tracesInitialDate: (date string formatted as "yyyy-mm-dd") Optional. Traces will be trimmed for dates earlier than provided value. Data before tracesInitialDate will not be included in the data.
tracesEndDate: (date string formatted as "yyyy-mm-dd") Optional. Traces will be trimmed for dates later than provided value. Data after tracesInitialDate will not be included in the data.
initialDateForInitialDisplay: (date string formatted as "yyyy-mm-dd") Optional. Date at which initial display will begin.
yearsToPlotBackFromCurrent or yearsToPlot: (number) Optional. Number of years to plot from current date backwards in the initial plot. This parameter will override the information from initialDateForInitialDisplay:.
yearsToPlotBackFromMaximum: (number) Optional. Number of years to plot from the Maximum date in the data set backwards in the initial plot. This parameter will override the information from initialDateForInitialDisplay or yearsToPlotBackFromCurrent or yearsToPlot.
endDateForInitialDisplay: (date string formatted as "yyyy-mm-dd") Optional. Date at which initial display of traces will end.
forecastMonthsFromCurrentDate: (number) Optional. This will set the date at which initial display of traces will end. This paremeter overrides the endDateForInitialDisplay.
Attention: If no endDateForInitialDisplay or forecastMonthsFromCurrentDate is provided, but yearsToPlotBackFromCurrent or yearsToPlot if provided, then the end date for initial display will be set to the current date.
in your javascript:
var timeInfo = {
// affects only the initial display,
// it will set the initial display to start one year before the current date and end on the current date
yearsToPlot: 1,
// include and initial date if applicable (data will be trimmed to before this date)
tracesInitialDate: "1998-12-31"
};
The layout object follows Plotly's definition.
Example: in your javascript:
// include layout as per Plotly's layout
var layout = {
yaxis: {
// the the type of yaxis
// in case log/linear button option set,
// this will be the initial yaxis type display
type: "log",
hoverformat: ".4r"
},
margin:{
r: 43
}
};
The options object follows Plotly's definition.
It's and optional parameter.
Example: in your javascript:
// include options as per Plotly's options
var **options** = {
// this will hide the link to plotly
showLink: false,
// this will hid the mode bar
displayModeBar: false
};
Include libraries for plotly, d3.queue, jszip (only if zip files from fred are to be sourced to update recessions (it may be omitted if url set to "") and aoPlotlyAddOn:
<head>
<!-- Plotly.js -->
<script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
<!-- d3.queue -->
<script src="https://d3js.org/d3-queue.v3.min.js"></script>
<!-- jszip.js -->
<script src="https://rawgit.com/Stuk/jszip/master/dist/jszip.min.js"></script>
<!-- aoPlotlyAddOn.js -->
<script src="https://raw.githubusercontent.com/ajoposor/aoPlotlyAddOn/master/dist/aoPlotlyAddOn.min.js"></script>
</head>
<head>
<script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
<script src="https://d3js.org/d3-queue.v3.min.js"></script>
<script src="https://rawgit.com/Stuk/jszip/master/dist/jszip.min.js"></script>
<script src="https://rawgit.com/ajoposor/aoPlotlyAddOn/master/dist/aoPlotlyAddOn.min.js"></script>
</head>
(function() {
// all the code for one plot here
})();
<head>
<!-- Plotly.js -->
<script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="https://rawgit.com/ajoposor/aoPlotlyAddOn/master/dist/aoPlotlyAddOn.min.js"></script>
</head>
<body>
<div id="myWholeDiv_01" style="visibility:hidden">
<h3>My Plot Title and header</h3>
<div id="myPlotlyDiv_01" class="plotly" align="left" style="width:100%; height:480px;"></div>
Plot footnotes<br>
Data source: <a href="https://www.quandl.com">Quandl.</a>
</div>
<script>
<!-- JAVASCRIPT CODE GOES HERE -->
</script>
</body>
(function() {
/* declare and populate variables with parameters
var divInfo = {
wholeDivID: "myWholeDiv_01",
plotDivID: "myPlotlyDiv_01"
};
var timeInfo = {
yearsToPlot: 1,
tracesInitialDate: "1998-12-31"
};
var data = [
{
type: "scatter",
name: "S&P 500",
mode: "lines",
opacity: 1,
fill: "tozeroy",
fillcolor: "rgba(205, 245,255, 0.20)", //#FAFCFD + 50% transparency
line: {
color: "#1A5488",
width: 3,
dash: "solid"
}
}, ...
];
var otherDataProperties = [
{
traceID: "S&P 500",
toggleRealNominal: true,
},...
];
var dataSources = [
{
urlType: "csv",
url: "https://rawgit.com/ajoposor/test-csv-files/master/files/SP500%20sectors-1998-12-2017-04.csv",
onlyAddXDateSuffix: " 00:00:00-04:00",
traces: [
{
xSeriesName: "Date",
ySeriesName: "SP500 Adjusted Close",
traceID: "S&P 500"
},...
]
}
];
var settings = {
series: {
baseFrequency: "daily",
baseAggregation: "close",
targetFrequencyDisplay: "daily",
},
displayRecessions: true,
allowCompare: true,
allowDownload: true,
allowRealNominal: true,
initialRealNominal: "real",
baseRealDate: "end of range",
downloadedFileName: "S&P Sectors Data",
xAxisNameOnCSV: "Date",
transformToBaseIndex: true,
allowFrequencyResampling: true,
desiredFrequencies: [
"daily",
"weekly",
"monthly",
"quarterly",
"semiannual",
"annual"
],
allowSelectorOptions: true, // buttons for time range selection, 3m, 6m, 1y, YTD, 5y, etc.
allowLogLinear: true,
textAndSpaceToTextRatio: 1.8,
endOfWeek: 5 // 0 Sunday, 1 Monday, etc.
};
var layout = {
// your layout options
};
var options = {
// your options
};
// call function
aoPlotlyAddOn.newTimeseriesPlot(
divInfo,
data,
otherDataProperties,
dataSources,
settings,
timeInfo,
layout,
options);
})();
This function will read data from a number of sources and save the processed data into yourDataArray in a ploltly style. This data can then be used to feed multiple instances of aoPlotlyAddOn.newTimeseriesPlot. The arguments have the same structure as for aoPlotlyAddOn.newTimeseriesPlot.
yourDataArray: (array of objecs) Required. Provide a dataArray with as many elements as traces to be read. See aoPlotlyAddOn.newTimeseriesPlot, data for further details.
otherDataProperties: (array of objecs) Required. See aoPlotlyAddOn.newTimeseriesPlot, otherDataParametes for further details.
dataSources: (array of objecs) Required. See aoPlotlyAddOn.newTimeseriesPlot, dataSources for further details.
settings: (object) Parameters applicable:
queueConcurrencyLimit: (positive integer) Optional. Default is 10. Sets the value for maximum concurrent async tasks for reading external data from urls. ATTENTION: in case you feed a trace from many sources, set queueConcurrencyLimit to 1, so that the assembling of the trace is made synchronously, otherwise it may result in incorrect trace data values.
dataReadFlag: (one element array) Required. Provides a flag to signal aoPlotlyAddOn.newTimeseriesPlot instances when the data has been read, so that aoPlotlyAddOn.newTimeseriesPlot can wait until aoPlotlyAddOn.readSomeDataSourcesIntoData has finished. See example below.
timeInfo: (object) Optional. See aoPlotlyAddOn.newTimeseriesPlot, timeInfo for further details.
myCallback: (function) Optional. In case you want to provide a callback, to be executed after aoPlotlyAddOn.readSomeDataSourcesIntoData finishes.
Example:
in your javascript:
// you will need a data array and a flag with a proper scope,
// i.e., that can be passed as argument to further instances of **aoPlotlyAddOn.newTimeseriesPlot**
var myReusableData = [{
name: "trace_1"
}, ...{
name: "trace_n"
}];
var myReusableDataReadFlag = [ false ];
// set and call the reusable data reading function
(function() {
var timeInfo = {
tracesInitialDate: "1960-01-01",
};
var settings = {
queueConcurrencyLimit: 1,
dataReadFlag: myReusableDataReadFlag,
};
var otherDataProperties = [ {
traceID: "traceID1"
}, ...{
traceID: "traceIDN"
}
];
var dataSources = [ {},
... {}
];
aoPlotlyAddOn.readSomeDataSourcesIntoData(data, dataSources, otherDataProperties,
dataSources, settings, timeInfo);
})();
// call as many instances of **aoPlotlyAddOn.newTimeseriesPlot** as required
(function() {
var divInfo = {
wholeDivID: "myWholeDivID",
plotDivID: "myDivID",
onErrorHideWholeDiv: false
};
var timeInfo = {
tracesInitialDate: "1960-01-01",
};
var data = [{
name: "my trace name"
}];
function loadDataCallback() {
data[0].x = aoPlotlyAddOn.arrayCopy(myReusableData[0].x);
data[0].y = aoPlotlyAddOn.arrayCopy(myReusableData[0].y);
}
var settings = {
queueConcurrencyLimit: 1,
dataReadFlag: myReusableDataReadFlag,
waitForGlobalData: true,
globalDataCallback: loadDataCallback
};
var otherDataProperties = [ {
traceID: "traceID1"
}
];
var dataSources = [ {},
... {}
];
aoPlotlyAddOn.newTimeSeriesPlot(divInfo, data, otherDataProperties, dataSources,
settings, timeInfo, layout, options);
})();
Returns a new array of values (shallow copy) which is a copy of the provided array. Use together with aoPlotlyAddOn.readSomeDataSourcesIntoData when assigning the readData to a new data for an instance of aoPlotlyAddOn.newTimeseriesPlot.
[ value 1, value 2, value 3, ... value n]
This function will update the library recessions array with new periods, using the FRED api. This can be used when creating multiple plots in a page, so that the api call is made only once. The url must return a zip file with the FRED USRECP serie, containing a txt file, with the format use by the fred api.
https://api.stlouisfed.org/fred/series/observations?series_id=USRECP&api_key=YourFredAPiKey&file_type=txt
This utility function returns a date (as a string with the format yyyy-mm-dd) that is a number of years, months or days ahead or before a specified date). Use it as help to feed the timeInfo object.
currentDateAsDate: Required. Pass new Date(), or any other date to be used as equivalent to currentDate.
requestCode: (string) Required. Any of "end of year", "end of month", "end of day". The shiftNumber will be used to calculated the date that is shiftNumber of years, months, or days ahead or before currentDateAsDate. It will return a date that is at the end of the corresponding period.
shiftNumber: (number, positive or negative) Number of years, months, or days ahead (positive) or before (negative) the currentDateAsDate.
This functions returns and object with the the tickvals and ticktext arrays for a specific time range, division width and margins, font type and size and a ratio for the space between ticks (specifically the ratio between (tick text + space between text) lenght to tick text length.
from: (date strings as "yyyy-mm-dd")
to: (date strings as "yyyy-mm-dd")
textAndSpaceToTextRatio: (number) Ratio between (tick text + space between text) lenght to tick text length.
targetFrequency: (string) Any of "daily", "everyOtherDay", "weekly", "biweekly", "monthly", "quarterly", "semiannual", "annual", "biennial", "quinquennial", "decennial", "quadranscentennial", "semicentennial", "centennial", "bicentennial", "sestercentennial", "quincentenary", "milennial"
The returned ticktext and tickvals would be the best minimum fit, upwards from the targetFrequency, e.g., if "monthly" is passed, it would return whichever fit best from monthly, quarterly, semiannual, annual and onwards
fontFamily: (string) Font family name.
fontSize: (number) Font size.
divWidth: (number) Width in pixels of the current division
leftMargin: (number) Margins from plot to division in pixels. If layout.margin.l/r are defined, read from there, otherwise use 80 (plotly's default)
rightMargin: (number) Margins from plot to division in pixels. If layout.margin.l/r are defined, read from there, otherwise use 80 (plotly's default)
This function will populate the data object with an originalData.x, y and objects for different frequencies and methods of aggregation. This data can be used to change the frequency of displayed data.
data: (array of data objects [{x[], y[]}, ....]) With x as date strings "yyyy-mm-dd" and y as values.
periodKeys: (object) An object with the frequencies to be calculated, set to true or false. { day: true/false, week: true/false, month: true/false, quarter: true/false, semester: true/false, year: true }
endOfWeek: (number between 0 and 6) Day of week to be end of week period. 0 for Sunday, 1 for Monday, ....
Github | ||
---|---|---|
A. Osorio | @ajoposor |
1.0.0 Function launched
1.1.0
Added:
Improved:
1.1.1
Code released under the MIT license.
Docs released under the Creative Commons license.