Skip to content
Open
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
93 changes: 57 additions & 36 deletions original/dygraph-extra.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ Dygraph.Export.DEFAULT_ATTRS = {

backgroundColor: "transparent",

//Texts displayed below the chart's x-axis and to the left of the y-axis
//Texts displayed below the chart's x-axis and to the left of the y-axis
titleFont: "bold 18px serif",
titleFontColor: "black",

//Texts displayed below the chart's x-axis and to the left of the y-axis
//Texts displayed below the chart's x-axis and to the left of the y-axis
axisLabelFont: "bold 14px serif",
axisLabelFontColor: "black",

Expand All @@ -49,11 +49,11 @@ Dygraph.Export.DEFAULT_ATTRS = {
maxlabelsWidth: 0,
labelTopMargin: 35,
magicNumbertop: 8

};

/**
* Tests whether the browser supports the canvas API and its methods for
* Tests whether the browser supports the canvas API and its methods for
* drawing text and exporting it as a data URL.
*/
Dygraph.Export.isSupported = function () {
Expand Down Expand Up @@ -93,18 +93,18 @@ Dygraph.Export.asPNG = function (dygraph, img, userOptions) {
*/
Dygraph.Export.asCanvas = function (dygraph, userOptions) {
"use strict";
var options = {},
canvas = Dygraph.createCanvas();
var options = {};
var canvas = document.createElement("canvas");

Dygraph.update(options, Dygraph.Export.DEFAULT_ATTRS);
Dygraph.update(options, userOptions);

canvas.width = dygraph.width_;
canvas.height = dygraph.height_ + options.legendHeight;

Dygraph.Export.drawPlot(canvas, dygraph, options);
Dygraph.Export.drawPlot(canvas, dygraph, options);
Dygraph.Export.drawLegend(canvas, dygraph, options);

return canvas;
};

Expand All @@ -121,22 +121,22 @@ Dygraph.Export.drawPlot = function (canvas, dygraph, options) {

// Copy the plot canvas into the context of the new image.
var plotCanvas = dygraph.hidden_;

var i = 0;

ctx.drawImage(plotCanvas, 0, 0);


// Add the x and y axes
var axesPluginDict = Dygraph.Export.getPlugin(dygraph, 'Axes Plugin');
if (axesPluginDict) {
var axesPlugin = axesPluginDict.plugin;

for (i = 0; i < axesPlugin.ylabels_.length; i++) {
Dygraph.Export.putLabel(ctx, axesPlugin.ylabels_[i], options,
options.labelFont, options.labelFontColor);
}

for (i = 0; i < axesPlugin.xlabels_.length; i++) {
Dygraph.Export.putLabel(ctx, axesPlugin.xlabels_[i], options,
options.labelFont, options.labelFontColor);
Expand All @@ -149,29 +149,43 @@ Dygraph.Export.drawPlot = function (canvas, dygraph, options) {
if (labelsPluginDict) {
var labelsPlugin = labelsPluginDict.plugin;

Dygraph.Export.putLabel(ctx, labelsPlugin.title_div_, options,
options.titleFont, options.titleFontColor);
if(labelsPlugin.title_div_ != undefined)
{
labelsPlugin.title_div_.style.textAlign = "center"
Dygraph.Export.putLabel(ctx, labelsPlugin.title_div_, options,
options.titleFont, options.titleFontColor);
}

Dygraph.Export.putLabel(ctx, labelsPlugin.xlabel_div_, options,
options.axisLabelFont, options.axisLabelFontColor);
if(labelsPlugin.xlabel_div_ != undefined)
{
labelsPlugin.xlabel_div_.style.textAlign = "center"
Dygraph.Export.putLabel(ctx, labelsPlugin.xlabel_div_, options,
options.axisLabelFont, options.axisLabelFontColor);
}

Dygraph.Export.putVerticalLabelY1(ctx, labelsPlugin.ylabel_div_, options,
options.axisLabelFont, options.axisLabelFontColor, "center");
if(labelsPlugin.ylabel_div_ != undefined)
{
Dygraph.Export.putVerticalLabelY1(ctx, labelsPlugin.ylabel_div_, options,
options.axisLabelFont, options.axisLabelFontColor, "center");
}

Dygraph.Export.putVerticalLabelY2(ctx, labelsPlugin.y2label_div_, options,
options.axisLabelFont, options.axisLabelFontColor, "center");
if(labelsPlugin.y2label_div_ != undefined)
{
Dygraph.Export.putVerticalLabelY2(ctx, labelsPlugin.y2label_div_, options,
options.axisLabelFont, options.axisLabelFontColor, "center");
}
}


for (i = 0; i < dygraph.layout_.annotations.length; i++) {
Dygraph.Export.putLabelAnn(ctx, dygraph.layout_.annotations[i], options,
Dygraph.Export.putLabelAnn(ctx, dygraph.layout_.annotations[i], options,
options.labelFont, options.labelColor);
}

};

/**
* Draws a label (axis label or graph title) at the same position
* Draws a label (axis label or graph title) at the same position
* where the div containing the text is located.
*/
Dygraph.Export.putLabel = function (ctx, divLabel, options, font, color) {
Expand All @@ -183,15 +197,15 @@ Dygraph.Export.putLabel = function (ctx, divLabel, options, font, color) {

var top = parseInt(divLabel.style.top, 10);
var left = parseInt(divLabel.style.left, 10);

if (!divLabel.style.top.length) {
var bottom = parseInt(divLabel.style.bottom, 10);
var height = parseInt(divLabel.style.height, 10);

top = ctx.canvas.height - options.legendHeight - bottom - height;
}

// FIXME: Remove this 'magic' number needed to get the line-height.
// FIXME: Remove this 'magic' number needed to get the line-height.
top = top + options.magicNumbertop;

var width = parseInt(divLabel.style.width, 10);
Expand All @@ -207,7 +221,7 @@ Dygraph.Export.putLabel = function (ctx, divLabel, options, font, color) {

Dygraph.Export.putText(ctx, left, top, divLabel, font, color);
};

/**
* Draws a label Y1 rotated 90 degrees counterclockwise.
*/
Expand Down Expand Up @@ -239,7 +253,7 @@ Dygraph.Export.putVerticalLabelY1 = function (ctx, divLabel, options, font, colo
ctx.font = font;
ctx.textAlign = textAlign;
ctx.fillText(text, top, left);

ctx.restore();
};

Expand All @@ -251,15 +265,15 @@ Dygraph.Export.putVerticalLabelY2 = function (ctx, divLabel, options, font, colo
if (!divLabel) {
return;
}

var top = parseInt(divLabel.style.top, 10);
var right = parseInt(divLabel.style.right, 10) + parseInt(divLabel.style.width, 10) * 2;
var text = divLabel.innerText || divLabel.textContent;

if (textAlign == "center") {
top = Math.ceil(ctx.canvas.height / 2);
}

ctx.save();
ctx.translate(parseInt(divLabel.style.width, 10), 0);
ctx.rotate(Math.PI / 2);
Expand All @@ -268,7 +282,7 @@ Dygraph.Export.putVerticalLabelY2 = function (ctx, divLabel, options, font, colo
ctx.font = font;
ctx.textAlign = textAlign;
ctx.fillText(text, top, right - ctx.canvas.width);

ctx.restore();
};

Expand All @@ -277,7 +291,7 @@ Dygraph.Export.putVerticalLabelY2 = function (ctx, divLabel, options, font, colo
*/
Dygraph.Export.putText = function (ctx, left, top, divLabel, font, color) {
"use strict";
var textAlign = divLabel.style.textAlign || "left";
var textAlign = divLabel.style.textAlign || "left";
var text = divLabel.innerText || divLabel.textContent;

ctx.fillStyle = color;
Expand All @@ -300,14 +314,14 @@ Dygraph.Export.drawLegend = function (canvas, dygraph, options) {

// Margin between labels
var labelMargin = 5;

var colors = dygraph.getColors();
// Drop the first element, which is the label for the time dimension
var labels = dygraph.attr_("labels").slice(1);

// 1. Compute the width of the labels:
var labelsWidth = 0;

var i;
for (i = 0; i < labels.length; i++) {
labelsWidth = labelsWidth + ctx.measureText("- " + labels[i]).width + labelMargin;
Expand All @@ -327,7 +341,7 @@ Dygraph.Export.drawLegend = function (canvas, dygraph, options) {
for (i = 0; i < labels.length; i++) {
if (labelVisibility[i]) {
//TODO Replace the minus sign by a proper dash, although there is a
// problem when the page encoding is different than the encoding
// problem when the page encoding is different than the encoding
// of this file (UTF-8).
var txt = "- " + labels[i];
ctx.fillStyle = colors[usedColorCount];
Expand All @@ -352,3 +366,10 @@ Dygraph.Export.getPlugin = function(dygraph, name) {
}
return null;
}

/**
* Updates newOptions with values from oldOptions
*/
Dygraph.update = function(newOptions, oldOptions) {
Object.assign(newOptions,oldOptions);
};