This repository has been archived by the owner on Nov 5, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SaveChartFactory.js
146 lines (125 loc) · 4.84 KB
/
SaveChartFactory.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
* Copyright (C) 2015 con terra GmbH (info@conterra.de)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
define(["dojo/_base/declare",
"dijit/_WidgetBase",
"dojo/dom-construct",
"cedar",
"ct/ui/desktop/util",
"ct/_Connect",
"dijit/form/Button",
"dojo/query",
"dojo/on"
],
function(declare,_WidgetBase,domConstruct,Cedar,ct_util,_Connect,Button, dojoQuery,on) {
return declare([_WidgetBase], {
openWindow : function(chart) {
if(chart != undefined){
var that = this.openWindow
var self = this
if (that.count == undefined || that.count == null){
that.count = 0
}
else{
that.count = that.count + 1
}
windowContent = domConstruct.create("div", {id:"Chart"+that.count})
var w = that.w = this.windowManager.createWindow({
title: "Saved Chart",
marginBox: {l: 30, t: 30, w: 900, h: 600},
minimizeOnClose: true,
closable: true,
content: windowContent,
showOnStartup : true,
windowClass: "myWindowClass"
});
w.get("window")
var window2 = w._getWindow()
window2.onResize = function(){
self.resize()
}
window2.onHide = function (){
self.onWindowClose()
}
function savedChart(chart, id){
this.chart = chart
this.id = id
}
var presentChart = new savedChart(chart, that.count)
if (!that.savedChartArray){
that.savedChartArray = []
that.savedChartArray.push(presentChart)
}
else {
that.savedChartArray.push(presentChart)
}
that.savedChartArray[that.count]["chart"].on('dblclick', function(event,item){
that.savedChartArray[that.count]["chart"]._definition.dataset.query.where = "1=1"
that.savedChartArray[that.count]["chart"].show({
elementId: "div#Chart" + that.count
})
})
that.savedChartArray[that.count]["chart"].show({
elementId: "div#Chart" + that.count
})
domConstruct.create("div",{id:"Chart"+that.count+"Button"},"Chart"+that.count,"after")
var button = new Button({
label:"Download Chart",
onClick: function(){self.downloadChart(that.count)},
},"Chart"+that.count+"Button").startup()
}},
onWindowClose: function (){
},
downloadChart: function(count){
var that = this.openWindow
var svg = {}
dojoQuery(".marks").forEach(function(node, index, arr) {
svg = node
})
var xml = new XMLSerializer().serializeToString(svg)
var data = "data:image/svg+xml;base64," + btoa(unescape(encodeURIComponent(xml)));
var canvas = domConstruct.create("canvas", {
width: 2000,
height: 2500,
id: "newChart2"
}, "Chart"+count , "chart"),
context = canvas.getContext("2d")
var img = new Image()
img.src = data
var drawImage = function() {
context.drawImage(img, 0, 0)
var canvasData = canvas.toDataURL("image/jpeg", 1, 0);
var chartLink2 = domConstruct.create("a", null,"Chart"+count);
chartLink2.href = canvasData
chartLink2.download = "MyChart"
chartLink2.click()
domConstruct.destroy("newChart2")
};
var onload = on(img, "load", drawImage());
},
resize: function(){
var that = this.openWindow
var enclosingWindow = that.w.window
var heightAfterResize = (enclosingWindow._contentBox.h)
var widthAfterResize = (enclosingWindow._contentBox.w)
that.savedChartArray[that.count]["chart"].show({
elementId: "div#Chart" + that.count,
renderer: "svg",
height: heightAfterResize,
width:widthAfterResize,
});
}
})
})