Debian Codegraph @

dojox/charting/SimpleTheme.js

raw symbols SCIP
1 define(["dojo/_base/lang", "dojo/_base/array","dojo/_base/declare","dojo/_base/Color", "dojox/lang/utils", "dojox/gfx/gradutils"],
2 function(lang, arr, declare, Color, dlu, dgg){
3
4 var SimpleTheme = declare("dojox.charting.SimpleTheme", null, {
5 // summary:
6 // A SimpleTheme or Theme is a pre-defined object, primarily JSON-based, that makes up the definitions to
7 // style a chart.
8 //
9 // description:
10 // While you can set up style definitions on a chart directly (usually through the various add methods
11 // on a dojox.charting.Chart object), a Theme simplifies this manual setup by allowing you to
12 // pre-define all of the various visual parameters of each element in a chart.
13 //
14 // Most of the properties of a Theme are straight-forward; if something is line-based (such as
15 // an axis or the ticks on an axis), they will be defined using basic stroke parameters. Likewise,
16 // if an element is primarily block-based (such as the background of a chart), it will be primarily
17 // fill-based.
18 //
19 // In addition (for convenience), a Theme definition does not have to contain the entire JSON-based
20 // structure. Each theme is built on top of a default theme (which serves as the basis for the theme
21 // "GreySkies"), and is mixed into the default theme object. This allows you to create a theme based,
22 // say, solely on colors for data series.
23 //
24 // Defining a new theme is relatively easy; see any of the themes in dojox.charting.themes for examples
25 // on how to define your own.
26 //
27 // When you set a theme on a chart, the theme itself is deep-cloned. This means that you cannot alter
28 // the theme itself after setting the theme value on a chart, and expect it to change your chart. If you
29 // are looking to make alterations to a theme for a chart, the suggestion would be to create your own
30 // theme, based on the one you want to use, that makes those alterations before it is applied to a chart.
31 //
32 // Finally, a Theme contains a number of functions to facilitate rendering operations on a chart--the main
33 // helper of which is the ~next~ method, in which a chart asks for the information for the next data series
34 // to be rendered.
35 //
36 // A note on colors:
37 // A theme palette is usually comprised of 5 different color definitions, and
38 // no more. If you have a need to render a chart with more than 5 data elements, you can simply "push"
39 // new color definitions into the theme's .color array. Make sure that you do that with the actual
40 // theme object from a Chart, and not in the theme itself (i.e. either do that before using .setTheme
41 // on a chart).
42 //
43 // example:
44 // The default theme (and structure) looks like so:
45 // | // all objects are structs used directly in dojox.gfx
46 // | chart:{
47 // | margins: {l: 10, t: 10, r: 10, b: 10},
48 // | stroke: null,
49 // | fill: "white",
50 // | pageStyle: null // suggested page style as an object suitable for dojo.style()
51 // | },
52 // | plotarea:{
53 // | stroke: null,
54 // | fill: "white"
55 // | },
56 // | axis:{
57 // | stroke: { // the axis itself
58 // | color: "#333",
59 // | width: 1
60 // | },
61 // | tick: { // used as a foundation for all ticks
62 // | color: "#666",
63 // | position: "center",
64 // | font: "normal normal normal 7pt Tahoma", // labels on axis
65 // | fontColor: "#333" // color of labels
66 // | },
67 // | majorTick: { // major ticks on axis, and used for major gridlines
68 // | width: 1,
69 // | length: 6
70 // | },
71 // | minorTick: { // minor ticks on axis, and used for minor gridlines
72 // | width: 0.8,
73 // | length: 3
74 // | },
75 // | microTick: { // minor ticks on axis, and used for minor gridlines
76 // | width: 0.5,
77 // | length: 1
78 // | },
79 // | title: {
80 // | gap: 15,
81 // | font: "normal normal normal 11pt Tahoma", // title font
82 // | fontColor: "#333", // title font color
83 // | orientation: "axis" // "axis": facing the axis, "away": facing away
84 // | }
85 // | },
86 // | series: {
87 // | stroke: {width: 1.5, color: "#333"}, // line
88 // | outline: {width: 0.1, color: "#ccc"}, // outline
89 // | //shadow: {dx: 1, dy: 1, width: 2, color: [0, 0, 0, 0.3]},
90 // | shadow: null, // no shadow
91 // | //filter: dojox/gfx/filters.createFilter(),
92 // | filter: null, // no filter, to use a filter you must use gfx SVG render and require dojox/gfx/svgext
93 // | fill: "#ccc", // fill, if appropriate
94 // | font: "normal normal normal 8pt Tahoma", // if there's a label
95 // | fontColor: "#000" // color of labels
96 // | labelWiring: {width: 1, color: "#ccc"}, // connect marker and target data item(slice, column, bar...)
97 // | },
98 // | marker: { // any markers on a series
99 // | symbol: "m-3,3 l3,-6 3,6 z", // symbol
100 // | stroke: {width: 1.5, color: "#333"}, // stroke
101 // | outline: {width: 0.1, color: "#ccc"}, // outline
102 // | shadow: null, // no shadow
103 // | fill: "#ccc", // fill if needed
104 // | font: "normal normal normal 8pt Tahoma", // label
105 // | fontColor: "#000"
106 // | },
107 // | grid: { // grid, when not present axis tick strokes are used instead
108 // | majorLine: { // major grid line
109 // | color: "#666",
110 // | width: 1,
111 // | length: 6
112 // | },
113 // | minorLine: { // minor grid line
114 // | color: "#666",
115 // | width: 0.8,
116 // | length: 3
117 // | },
118 // | fill: "grey", // every other stripe
119 // | alternateFill: "grey" // alternate stripe
120 // | },
121 // | indicator: {
122 // | lineStroke: {width: 1.5, color: "#333"}, // line
123 // | lineOutline: {width: 0.1, color: "#ccc"}, // line outline
124 // | lineShadow: null, // no line shadow
125 // | lineFill: null, // fill between lines for dual indicators
126 // | stroke: {width: 1.5, color: "#333"}, // label background stroke
127 // | outline: {width: 0.1, color: "#ccc"}, // label background outline
128 // | shadow: null, // no label background shadow
129 // | fill: "#ccc", // label background fill
130 // | radius: 3, // radius of the label background
131 // | font: "normal normal normal 10pt Tahoma", // label font
132 // | fontColor: "#000" // label color
133 // | markerFill: "#ccc", // marker fill
134 // | markerSymbol: "m-3,0 c0,-4 6,-4 6,0 m-6,0 c0,4 6,4 6,0", // marker symbol
135 // | markerStroke: {width: 1.5, color: "#333"}, // marker stroke
136 // | markerOutline: {width: 0.1, color: "#ccc"}, // marker outline
137 // | markerShadow: null // no marker shadow
138 // | },
139 // | pieInnerRadius: 33
140 //
141 // example:
142 // Defining a new theme is pretty simple:
143 // | var Grasslands = new SimpleTheme({
144 // | colors: [ "#70803a", "#dde574", "#788062", "#b1cc5d", "#eff2c2" ],
145 // | pieInnerRadius: 15
146 // | });
147 // |
148 // | myChart.setTheme(Grasslands);
149
150 shapeSpaces: {shape: 1, shapeX: 1, shapeY: 1},
151
152 constructor: function(kwArgs){
153 // summary:
154 // Initialize a theme using the keyword arguments. Note that the arguments
155 // look like the example (above), and may include a few more parameters.
156 kwArgs = kwArgs || {};
157
158 // populate theme with defaults updating them if needed
159 var def = SimpleTheme.defaultTheme;
160 arr.forEach(["chart", "plotarea", "axis", "grid", "series", "marker", "indicator"], function(name){
161 this[name] = lang.delegate(def[name], kwArgs[name]);
162 }, this);
163
164 // personalize theme
165 if(kwArgs.seriesThemes && kwArgs.seriesThemes.length){
166 this.colors = null;
167 this.seriesThemes = kwArgs.seriesThemes.slice(0);
168 }else{
169 this.seriesThemes = null;
170 this.colors = (kwArgs.colors || SimpleTheme.defaultColors).slice(0);
171 }
172 this.markerThemes = null;
173 if(kwArgs.markerThemes && kwArgs.markerThemes.length){
174 this.markerThemes = kwArgs.markerThemes.slice(0);
175 }
176 this.markers = kwArgs.markers ? lang.clone(kwArgs.markers) : lang.delegate(SimpleTheme.defaultMarkers);
177
178 // set flags
179 this.noGradConv = kwArgs.noGradConv;
180 this.noRadialConv = kwArgs.noRadialConv;
181 if(kwArgs.reverseFills){
182 this.reverseFills();
183 }
184
185 // private housekeeping
186 this._current = 0;
187 this._buildMarkerArray();
188 },
189
190 clone: function(){
191 // summary:
192 // Clone the current theme.
193 // returns: dojox.charting.SimpleTheme
194 // The cloned theme; any alterations made will not affect the original.
195 var theme = new this.constructor({
196 // theme components
197 chart: this.chart,
198 plotarea: this.plotarea,
199 axis: this.axis,
200 grid: this.grid,
201 series: this.series,
202 marker: this.marker,
203 // individual arrays
204 colors: this.colors,
205 markers: this.markers,
206 indicator: this.indicator,
207 seriesThemes: this.seriesThemes,
208 markerThemes: this.markerThemes,
209 // flags
210 noGradConv: this.noGradConv,
211 noRadialConv: this.noRadialConv,
212 pieInnerRadius: this.pieInnerRadius
213 });
214 // copy custom methods
215 arr.forEach(
216 ["clone", "clear", "next", "skip", "addMixin", "post", "getTick"],
217 function(name){
218 if(this.hasOwnProperty(name)){
219 theme[name] = this[name];
220 }
221 },
222 this
223 );
224 return theme; // dojox.charting.SimpleTheme
225 },
226
227 clear: function(){
228 // summary:
229 // Clear and reset the internal pointer to start fresh.
230 this._current = 0;
231 },
232
233 next: function(elementType, mixin, doPost){
234 // summary:
235 // Get the next color or series theme.
236 // elementType: String?
237 // An optional element type (for use with series themes)
238 // mixin: Object?
239 // An optional object to mix into the theme.
240 // doPost: Boolean?
241 // A flag to post-process the results.
242 // returns: Object
243 // An object of the structure { series, marker, symbol }
244 var merge = dlu.merge, series, marker;
245 if(this.colors){
246 series = lang.delegate(this.series);
247 marker = lang.delegate(this.marker);
248 var color = new Color(this.colors[this._current % this.colors.length]), old;
249 // modify the stroke
250 if(series.stroke && series.stroke.color){
251 series.stroke = lang.delegate(series.stroke);
252 old = new Color(series.stroke.color);
253 series.stroke.color = new Color(color);
254 series.stroke.color.a = old.a;
255 }else{
256 series.stroke = {color: color};
257 }
258 if(marker.stroke && marker.stroke.color){
259 marker.stroke = lang.delegate(marker.stroke);
260 old = new Color(marker.stroke.color);
261 marker.stroke.color = new Color(color);
262 marker.stroke.color.a = old.a;
263 }else{
264 marker.stroke = {color: color};
265 }
266 // modify the fill
267 if(!series.fill || series.fill.type){
268 series.fill = color;
269 }else{
270 old = new Color(series.fill);
271 series.fill = new Color(color);
272 series.fill.a = old.a;
273 }
274 if(!marker.fill || marker.fill.type){
275 marker.fill = color;
276 }else{
277 old = new Color(marker.fill);
278 marker.fill = new Color(color);
279 marker.fill.a = old.a;
280 }
281 }else{
282 series = this.seriesThemes ?
283 merge(this.series, this.seriesThemes[this._current % this.seriesThemes.length]) :
284 this.series;
285 marker = this.markerThemes ?
286 merge(this.marker, this.markerThemes[this._current % this.markerThemes.length]) :
287 series;
288 }
289
290 var symbol = marker && marker.symbol || this._markers[this._current % this._markers.length];
291
292 var theme = {series: series, marker: marker, symbol: symbol};
293
294 // advance the counter
295 ++this._current;
296
297 if(mixin){
298 theme = this.addMixin(theme, elementType, mixin);
299 }
300 if(doPost){
301 theme = this.post(theme, elementType);
302 }
303
304 return theme; // Object
305 },
306
307 skip: function(){
308 // summary:
309 // Skip the next internal color.
310 ++this._current;
311 },
312
313 addMixin: function(theme, elementType, mixin, doPost){
314 // summary:
315 // Add a mixin object to the passed theme and process.
316 // theme: dojox/charting/SimpleTheme
317 // The theme to mixin to.
318 // elementType: String
319 // The type of element in question. Can be "line", "bar" or "circle"
320 // mixin: Object|Array
321 // The object or objects to mix into the theme.
322 // doPost: Boolean
323 // If true, run the new theme through the post-processor.
324 // returns: dojox/charting/SimpleTheme
325 // The new theme.
326 if(lang.isArray(mixin)){
327 arr.forEach(mixin, function(m){
328 theme = this.addMixin(theme, elementType, m);
329 }, this);
330 }else{
331 var t = {};
332 if("color" in mixin){
333 if(elementType == "line" || elementType == "area"){
334 lang.setObject("series.stroke.color", mixin.color, t);
335 lang.setObject("marker.stroke.color", mixin.color, t);
336 }else{
337 lang.setObject("series.fill", mixin.color, t);
338 }
339 }
340 arr.forEach(["stroke", "outline", "shadow", "fill", "filter", "font", "fontColor", "labelWiring"], function(name){
341 var markerName = "marker" + name.charAt(0).toUpperCase() + name.substr(1),
342 b = markerName in mixin;
343 if(name in mixin){
344 lang.setObject("series." + name, mixin[name], t);
345 if(!b){
346 lang.setObject("marker." + name, mixin[name], t);
347 }
348 }
349 if(b){
350 lang.setObject("marker." + name, mixin[markerName], t);
351 }
352 });
353 if("marker" in mixin){
354 t.symbol = mixin.marker;
355 t.symbol = mixin.marker;
356 }
357 theme = dlu.merge(theme, t);
358 }
359 if(doPost){
360 theme = this.post(theme, elementType);
361 }
362 return theme; // dojox/charting/SimpleTheme
363 },
364
365 post: function(theme, elementType){
366 // summary:
367 // Process any post-shape fills.
368 // theme: dojox/charting/SimpleTheme
369 // The theme to post process with.
370 // elementType: String
371 // The type of element being filled. Can be "bar" or "circle".
372 // returns: dojox/charting/SimpleTheme
373 // The post-processed theme.
374 var fill = theme.series.fill, t;
375 if(!this.noGradConv && this.shapeSpaces[fill.space] && fill.type == "linear"){
376 if(elementType == "bar"){
377 // transpose start and end points
378 t = {
379 x1: fill.y1,
380 y1: fill.x1,
381 x2: fill.y2,
382 y2: fill.x2
383 };
384 }else if(!this.noRadialConv && fill.space == "shape" && (elementType == "slice" || elementType == "circle")){
385 // switch to radial
386 t = {
387 type: "radial",
388 cx: 0,
389 cy: 0,
390 r: 100
391 };
392 }
393 if(t){
394 return dlu.merge(theme, {series: {fill: t}});
395 }
396 }
397 return theme; // dojox/charting/SimpleTheme
398 },
399
400 getTick: function(name, mixin){
401 // summary:
402 // Calculates and merges tick parameters.
403 // name: String
404 // Tick name, can be "major", "minor", or "micro".
405 // mixin: Object?
406 // Optional object to mix in to the tick.
407 var tick = this.axis.tick, tickName = name + "Tick",
408 merge = dlu.merge;
409 if(tick){
410 if(this.axis[tickName]){
411 tick = merge(tick, this.axis[tickName]);
412 }
413 }else{
414 tick = this.axis[tickName];
415 }
416 if(mixin){
417 if(tick){
418 if(mixin[tickName]){
419 tick = merge(tick, mixin[tickName]);
420 }
421 }else{
422 tick = mixin[tickName];
423 }
424 }
425 return tick; // Object
426 },
427
428 inspectObjects: function(f){
429 arr.forEach(["chart", "plotarea", "axis", "grid", "series", "marker", "indicator"], function(name){
430 f(this[name]);
431 }, this);
432 if(this.seriesThemes){
433 arr.forEach(this.seriesThemes, f);
434 }
435 if(this.markerThemes){
436 arr.forEach(this.markerThemes, f);
437 }
438 },
439
440 reverseFills: function(){
441 this.inspectObjects(function(o){
442 if(o && o.fill){
443 o.fill = dgg.reverse(o.fill);
444 }
445 });
446 },
447
448 addMarker:function(/*String*/ name, /*String*/ segment){
449 // summary:
450 // Add a custom marker to this theme.
451 // example:
452 // | myTheme.addMarker("Ellipse", foo);
453 this.markers[name] = segment;
454 this._buildMarkerArray();
455 },
456
457 setMarkers:function(/*Object*/ obj){
458 // summary:
459 // Set all the markers of this theme at once. obj should be a
460 // dictionary of keys and path segments.
461 //
462 // example:
463 // | myTheme.setMarkers({ "CIRCLE": foo });
464 this.markers = obj;
465 this._buildMarkerArray();
466 },
467
468 _buildMarkerArray: function(){
469 this._markers = [];
470 for(var p in this.markers){
471 this._markers.push(this.markers[p]);
472 }
473 }
474 });
475
476 lang.mixin(SimpleTheme, {
477 defaultMarkers: {
478 CIRCLE: "m-3,0 c0,-4 6,-4 6,0 m-6,0 c0,4 6,4 6,0",
479 SQUARE: "m-3,-3 l0,6 6,0 0,-6 z",
480 DIAMOND: "m0,-3 l3,3 -3,3 -3,-3 z",
481 CROSS: "m0,-3 l0,6 m-3,-3 l6,0",
482 X: "m-3,-3 l6,6 m0,-6 l-6,6",
483 TRIANGLE: "m-3,3 l3,-6 3,6 z",
484 TRIANGLE_INVERTED: "m-3,-3 l3,6 3,-6 z"
485 },
486
487 defaultColors:[
488 // gray skies
489 "#54544c", "#858e94", "#6e767a", "#948585", "#474747"
490 ],
491
492 defaultTheme: {
493 // all objects are structs used directly in dojox.gfx
494 chart:{
495 stroke: null,
496 fill: "white",
497 pageStyle: null,
498 titleGap: 20,
499 titlePos: "top",
500 titleFont: "normal normal bold 14pt Tahoma", // chart title
501 titleFontColor: "#333",
502 titleAlign: "middle"
503 },
504 plotarea:{
505 stroke: null,
506 fill: "white"
507 },
508 // TODO: label rotation on axis
509 axis:{
510 stroke: { // the axis itself
511 color: "#333",
512 width: 1
513 },
514 tick: { // used as a foundation for all ticks
515 color: "#666",
516 position: "center",
517 font: "normal normal normal 7pt Tahoma", // labels on axis
518 fontColor: "#333", // color of labels
519 labelGap: 4 // gap between a tick and its label in pixels
520 },
521 majorTick: { // major ticks on axis, and used for major gridlines
522 width: 1,
523 length: 6
524 },
525 minorTick: { // minor ticks on axis, and used for minor gridlines
526 width: 0.8,
527 length: 3
528 },
529 microTick: { // minor ticks on axis, and used for minor gridlines
530 width: 0.5,
531 length: 1
532 },
533 title: {
534 gap: 15,
535 font: "normal normal normal 11pt Tahoma", // title font
536 fontColor: "#333", // title font color
537 orientation: "axis" // "axis": facing the axis, "away": facing away
538 }
539 },
540 series: {
541 // used as a "main" theme for series, sThemes augment it
542 stroke: {width: 1.5, color: "#333"}, // line
543 outline: {width: 0.1, color: "#ccc"}, // outline
544 //shadow: {dx: 1, dy: 1, width: 2, color: [0, 0, 0, 0.3]},
545 shadow: null, // no shadow
546 fill: "#ccc", // fill, if appropriate
547 font: "normal normal normal 8pt Tahoma", // if there's a label
548 fontColor: "#000", // color of labels
549 labelWiring: {width: 1, color: "#ccc"} // connect marker and target data item(slice, column, bar...)
550 },
551 marker: { // any markers on a series
552 stroke: {width: 1.5, color: "#333"}, // stroke
553 outline: {width: 0.1, color: "#ccc"}, // outline
554 //shadow: {dx: 1, dy: 1, width: 2, color: [0, 0, 0, 0.3]},
555 shadow: null, // no shadow
556 fill: "#ccc", // fill if needed
557 font: "normal normal normal 8pt Tahoma", // label
558 fontColor: "#000"
559 },
560 indicator: {
561 lineStroke: {width: 1.5, color: "#333"},
562 lineOutline: {width: 0.1, color: "#ccc"},
563 lineShadow: null,
564 lineFill: null,
565 stroke: {width: 1.5, color: "#333"},
566 outline: {width: 0.1, color: "#ccc"},
567 shadow: null,
568 fill : "#ccc",
569 radius: 3,
570 font: "normal normal normal 10pt Tahoma",
571 fontColor: "#000",
572 markerFill: "#ccc",
573 markerSymbol: "m-3,0 c0,-4 6,-4 6,0 m-6,0 c0,4 6,4 6,0",
574 markerStroke: {width: 1.5, color: "#333"},
575 markerOutline: {width: 0.1, color: "#ccc"},
576 markerShadow: null
577 }
578 }
579 });
580
581 return SimpleTheme;
582 });