apexcharter/inst/htmlwidgets/apexcharter.js

277 lines
8.1 KiB
JavaScript
Raw Normal View History

2018-07-30 22:54:39 +02:00
HTMLWidgets.widget({
2020-03-04 15:14:02 +01:00
name: "apexcharter",
2018-07-30 22:54:39 +02:00
2020-03-04 15:14:02 +01:00
type: "output",
2018-07-30 22:54:39 +02:00
factory: function(el, width, height) {
2019-02-14 17:40:03 +01:00
var ax_opts;
2019-02-14 17:17:30 +01:00
var apexchart = null;
2018-07-30 22:54:39 +02:00
return {
renderValue: function(x) {
// Global options
ax_opts = x.ax_opts;
// Sizing
2020-03-03 20:05:03 +01:00
if (typeof ax_opts.chart === "undefined") {
2018-08-01 23:02:29 +02:00
ax_opts.chart = {};
}
2018-07-30 22:54:39 +02:00
ax_opts.chart.width = width;
ax_opts.chart.height = height;
2020-03-03 20:05:03 +01:00
if (!ax_opts.chart.hasOwnProperty("parentHeightOffset")) {
2019-08-20 14:27:40 +02:00
ax_opts.chart.parentHeightOffset = 0;
}
2020-03-04 15:14:02 +01:00
2020-03-04 22:52:13 +01:00
if (x.hasOwnProperty("shinyEvents") & HTMLWidgets.shinyMode) {
2020-03-03 20:05:03 +01:00
if (!ax_opts.hasOwnProperty("chart")) {
ax_opts.chart = {};
}
if (!ax_opts.chart.hasOwnProperty("events")) {
ax_opts.chart.events = {};
}
2020-03-04 22:52:13 +01:00
if (x.shinyEvents.hasOwnProperty("click")) {
2020-03-04 15:14:02 +01:00
ax_opts.chart.events.dataPointSelection = function(
event,
chartContext,
opts
) {
2020-03-04 23:49:50 +01:00
var options = opts;
2020-03-14 20:41:41 +01:00
var nonEmpty = opts.selectedDataPoints.filter(function(el) {
2020-03-04 23:49:50 +01:00
return el !== null && el.length > 0;
});
2020-03-14 20:41:41 +01:00
console.log(chartContext);
2020-03-04 23:49:50 +01:00
if (nonEmpty.length > 0) {
var select = {};
for (var i = 0; i < opts.selectedDataPoints.length; i++) {
if (typeof opts.selectedDataPoints[i] === "undefined") {
continue;
}
2020-03-17 12:22:31 +01:00
var selection = getSelection(chartContext, options.selectedDataPoints, i);
2020-03-04 23:49:50 +01:00
if (selection !== null) {
if (opts.w.config.series[i].hasOwnProperty("name")) {
var name = opts.w.config.series[i].name;
select[name] = selection;
} else {
select[i] = selection;
}
}
2020-03-04 15:14:02 +01:00
}
2020-03-04 23:49:50 +01:00
if (is_single(options)) {
select = select[Object.keys(select)[0]];
2020-03-04 15:14:02 +01:00
}
2020-03-14 20:41:41 +01:00
Shiny.setInputValue(
x.shinyEvents.click.inputId + ":apex_click",
2020-03-17 18:05:41 +01:00
{value: select, datetime: is_datetime(chartContext)}
2020-03-14 20:41:41 +01:00
);
2020-03-04 23:49:50 +01:00
} else {
Shiny.setInputValue(x.shinyEvents.click.inputId, null);
2020-03-04 10:42:10 +01:00
}
2020-03-03 20:05:03 +01:00
};
}
2020-03-04 23:49:50 +01:00
if (x.shinyEvents.hasOwnProperty("zoomed")) {
2020-03-04 19:02:01 +01:00
ax_opts.chart.events.zoomed = function(chartContext, xaxis, yaxis) {
2020-03-04 23:49:50 +01:00
var id = x.shinyEvents.zoomed.inputId;
2020-03-14 20:41:41 +01:00
if (is_datetime(chartContext)) {
2020-03-04 19:02:01 +01:00
id = id + ":apex_datetime";
}
Shiny.setInputValue(id, {
x: getXaxis(xaxis),
y: getYaxis(xaxis)
});
};
}
2020-03-18 12:30:48 +01:00
if (x.shinyEvents.hasOwnProperty("selection")) {
ax_opts.chart.events.selection = function(chartContext, xaxis, yaxis) {
console.log(xaxis);
var id = x.shinyEvents.selection.inputId;
if (is_datetime(chartContext)) {
id = id + ":apex_datetime";
}
var selectionValue;
if (x.shinyEvents.selection.type == "x") {
selectionValue = {x: xaxis.xaxis};
} else if (x.shinyEvents.selection.type == "xy") {
2020-03-18 17:57:58 +01:00
selectionValue = {x: xaxis.xaxis, y: getYaxis(xaxis)};
2020-03-18 12:30:48 +01:00
} else if (x.shinyEvents.selection.type == "y") {
2020-03-18 17:57:58 +01:00
selectionValue = {y: getYaxis(xaxis)};
2020-03-18 12:30:48 +01:00
}
Shiny.setInputValue(id, selectionValue);
};
}
2020-03-03 20:05:03 +01:00
}
2018-07-30 22:54:39 +02:00
2019-02-14 17:17:30 +01:00
// Generate or update chart
if (apexchart === null) {
2020-02-12 11:57:24 +01:00
apexchart = new ApexCharts(el, ax_opts);
2019-02-14 17:17:30 +01:00
apexchart.render();
} else {
2019-02-15 23:33:40 +01:00
if (x.auto_update) {
2020-03-04 15:14:02 +01:00
apexchart.updateSeries(
ax_opts.series,
x.auto_update.series_animate
);
2020-02-12 18:21:40 +01:00
if (x.auto_update.update_options) {
apexchart.updateOptions(
2020-03-04 15:14:02 +01:00
ax_opts,
x.auto_update.options_redrawPaths,
2020-02-12 18:21:40 +01:00
x.auto_update.options_animate
);
2020-02-12 11:57:24 +01:00
}
2019-02-15 23:33:40 +01:00
} else {
apexchart.destroy();
2020-02-12 11:57:24 +01:00
apexchart = new ApexCharts(el, ax_opts);
2019-02-15 23:33:40 +01:00
apexchart.render();
}
2019-02-14 17:17:30 +01:00
}
2018-07-30 22:54:39 +02:00
},
2020-03-04 15:14:02 +01:00
getChart: function() {
2019-02-14 17:40:03 +01:00
return apexchart;
2018-09-07 18:06:41 +02:00
},
2018-07-30 22:54:39 +02:00
resize: function(width, height) {
2019-02-14 17:40:03 +01:00
apexchart.updateOptions({
2018-07-30 22:54:39 +02:00
chart: {
width: width,
height: height
}
});
}
};
}
});
2018-09-07 18:06:41 +02:00
// From Friss tuto (https://github.com/FrissAnalytics/shinyJsTutorials/blob/master/tutorials/tutorial_03.Rmd)
2020-03-04 15:14:02 +01:00
function get_widget(id) {
2018-09-07 18:06:41 +02:00
// Get the HTMLWidgets object
var htmlWidgetsObj = HTMLWidgets.find("#" + id);
2020-03-04 15:14:02 +01:00
2018-09-07 18:06:41 +02:00
// Use the getChart method we created to get the underlying billboard chart
2020-03-04 15:14:02 +01:00
var widgetObj;
if (typeof htmlWidgetsObj != "undefined") {
2018-09-07 18:06:41 +02:00
widgetObj = htmlWidgetsObj.getChart();
}
2020-03-04 15:14:02 +01:00
return widgetObj;
2018-09-07 18:06:41 +02:00
}
2020-03-04 23:49:50 +01:00
function is_single(options) {
2020-03-04 15:14:02 +01:00
var typeLabels = ["pie", "radialBar", "donut"];
2020-03-04 23:49:50 +01:00
var lab = typeLabels.indexOf(options.w.config.chart.type) > -1;
var single = options.w.config.series.length == 1;
2020-03-04 15:14:02 +01:00
return lab | single;
}
2018-09-07 18:06:41 +02:00
2020-03-14 20:41:41 +01:00
function is_datetime(chartContext) {
if (
chartContext.hasOwnProperty("w") &&
chartContext.w.hasOwnProperty("config") &&
chartContext.w.config.hasOwnProperty("xaxis") &&
chartContext.w.config.xaxis.hasOwnProperty("type")
) {
return chartContext.w.config.xaxis.type == "datetime";
} else {
return false;
}
}
2020-03-17 12:22:31 +01:00
function getSelection(chartContext, selectedDataPoints, serieIndex) {
2020-03-04 15:14:02 +01:00
var typeLabels = ["pie", "radialBar", "donut"];
var typeXY = ["scatter", "bubble"];
var selected;
2020-03-17 12:22:31 +01:00
if (typeLabels.indexOf(chartContext.opts.chart.type) > -1) {
var labels = chartContext.opts.labels;
selected = selectedDataPoints[serieIndex].map(function(index) {
2020-03-04 15:14:02 +01:00
return labels[index];
});
} else {
2020-03-17 12:22:31 +01:00
var data = chartContext.opts.series[serieIndex].data;
selected = selectedDataPoints[serieIndex].map(function(index) {
2020-03-04 15:14:02 +01:00
var val = data[index];
2020-03-17 12:22:31 +01:00
if (typeXY.indexOf(chartContext.opts.chart.type) < 0) {
2020-03-04 15:14:02 +01:00
if (val.hasOwnProperty("x")) {
val = val.x;
} else {
val = val[0];
}
}
return val;
});
}
2020-03-14 20:41:41 +01:00
//console.log(selected);
2020-03-17 12:22:31 +01:00
if (typeXY.indexOf(chartContext.opts.chart.type) > -1) {
2020-03-04 15:14:02 +01:00
selected = {
x: selected.map(function(obj) {
return obj.x;
}),
y: selected.map(function(obj) {
return obj.y;
})
};
}
2020-03-04 23:49:50 +01:00
if (typeof selected == "undefined") {
selected = null;
}
2020-03-04 15:14:02 +01:00
return selected;
}
2018-09-07 18:06:41 +02:00
2020-03-04 19:02:01 +01:00
function getYaxis(axis) {
var yzoom = { min: null, max: null };
2020-03-18 17:57:58 +01:00
if (typeof axis.yaxis != "undefined" && axis.yaxis !== null) {
var y_axis;
if (axis.yaxis.hasOwnProperty("min")) {
y_axis = axis.yaxis;
} else {
y_axis = axis.yaxis[0];
}
2020-03-04 19:02:01 +01:00
if (y_axis.hasOwnProperty("min") && typeof y_axis.min != "undefined") {
yzoom.min = y_axis.min;
}
if (y_axis.hasOwnProperty("max") && typeof y_axis.max != "undefined") {
yzoom.max = y_axis.max;
}
}
return yzoom;
}
function getXaxis(axis) {
var xzoom = { min: null, max: null };
if (typeof axis.xaxis != "undefined") {
var x_axis = axis.xaxis;
if (x_axis.hasOwnProperty("min") && typeof x_axis.min != "undefined") {
xzoom.min = x_axis.min;
}
if (x_axis.hasOwnProperty("max") && typeof x_axis.max != "undefined") {
xzoom.max = x_axis.max;
}
}
return xzoom;
}
2018-09-07 18:06:41 +02:00
if (HTMLWidgets.shinyMode) {
2019-07-19 16:00:10 +02:00
// update serie
2020-03-04 15:14:02 +01:00
Shiny.addCustomMessageHandler("update-apexchart-series", function(obj) {
var chart = get_widget(obj.id);
if (typeof chart != "undefined") {
chart.updateSeries(
[
{
data: obj.data.newSeries
}
],
obj.data.animate
);
}
2019-07-19 16:00:10 +02:00
});
2020-03-04 15:14:02 +01:00
2019-07-19 16:00:10 +02:00
// update options
2020-03-04 15:14:02 +01:00
Shiny.addCustomMessageHandler("update-apexchart-options", function(obj) {
var chart = get_widget(obj.id);
if (typeof chart != "undefined") {
chart.updateOptions(obj.data.options);
}
2018-09-07 18:06:41 +02:00
});
}