-
Notifications
You must be signed in to change notification settings - Fork 89
/
DefaultChartForPAD.R
775 lines (623 loc) · 26.8 KB
/
DefaultChartForPAD.R
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
# Generate Default Charts for PADS using heuristics
# Author: Jitender Aswani, Co-Founder @datadolph.in
# Date: 3/15/2013
# Copyright (c) 2011, under the Creative Commons Attribution-NonCommercial 3.0 Unported (CC BY-NC 3.0) License
# For more information see: https://creativecommons.org/licenses/by-nc/3.0/
# All rights reserved.
###
# Rules based - time series data - just two columns -
#
#
###
#
# Get default info for pad
#
getDefault <- function(){
return(list(dim="", mea="", size="", yaxis=list(), xaxis=list(), chart=list()))
}
#
# set default mea
#
setDefaultMea <- function(default,default.mea=""){
default$mea <- default.mea
return(default)
}
#
# set default dim
#
setDefaultDim <- function(default,default.dim=""){
default$dim <- default.dim
return(default)
}
#
# set pad type
#
setPadType <- function(default, pad.type="timeseries"){
default$type <- pad.type
return(default)
}
#
# set yaxis title
#
setYaxisTitle <- function(default, yaxis.title=""){
default$yaxis$title <- yaxis.title
return(default)
}
#
# set xaxis title
#
setXaxisTitle <- function(default, xaxis.title=""){
default$xaxis$title <- xaxis.title
return(default)
}
#
# set xaxis categories
#
setXaxisCat <- function(default, xaxis.categories=c("")){
default$xaxis$categories <- as.character(xaxis.categories)
return(default)
}
#
# set default size
#
setSizeTitle <- function(default, size.title){
default$size <- as.character(size.title)
return(default)
}
#
# setChartType
#
setChartType <- function(default, chart.type="line"){
default$chart$type=chart.type
return(default)
}
#
# setChartType Stacked for bar and column
#
setChartTypeStacked <- function(default){
default$chart$stacked=1
return(default)
}
#
# setChartTitle
#
setChartTitle <- function(default, chart.title="line"){
default$chart$title=chart.title
return(default)
}
#
# add series
#
addSeries <- function(default, series.name, series.data) {
default$chart$series[[length(default$chart$series)+1]] <- list(name=series.name, data=series.data)
return(default)
}
#
# get histograms for a pad containing single column
#
getChartForSingleColPad <- function(pad, pmd) {
#one colummn - prepare a histogram
if(verbose) print("PAD has only one column, getting a histogram prepared")
if(log.all) logMessage("PAD has only one column, getting a histogram prepared")
# Initialize
histo <- NULL
default <- getDefault()
#is the column numeric (i.e. mlist exists)
if((length(pmd$mList) > 0) && (length(pmd$dList)==0)) {
#h <- hist(pads[1], breaks=8, plot=F)
histo <- as.data.frame(table(cut(pad[,1], breaks=8)))
#change col names
colnames(histo) <- c("groups", "count")
#set default mea
default <- setDefaultMea(default, names(pmd$mList)[1])
} else if((length(pmd$dList) > 0) && (length(pmd$mList)==0)) {
#is the column character (i.e. mlist exists)
histo <- as.data.frame(table(pad[,1]), stringsAsFactors=F)[1:15,]
#change col names
colnames(histo) <- c(names(pmd$dList)[1], "count")
#sort based on frequency
histo <- histo[with(histo, order(-count)), ]
#set default dim
default <- setDefaultDim(default, names(pmd$dList)[1])
}
#set pad type
default <- setPadType(default, "single column")
#set yaxis title
default <- setYaxisTitle(default, "Count (Frequency)")
#set xaxis tile and categories
default <- setXaxisTitle(default, names(histo)[1])
default <- setXaxisCat(default, histo[,1])
#print(default)
#set chart type
default <- setChartType(default, "column")
default <- setChartTitle(default,paste("Frequency Plot of", names(pad)[1], sep=" "))
#add a series
default.series.data <- as.matrix(histo)
dimnames(default.series.data) <- NULL
default <- addSeries(default,"Count (Frequency)", default.series.data)
#print(default)
return(default);
}
#
# get chart for a pad containing two columns
#
getChartForTwoColPad <- function(pad, pmd) {
# 2 column in this pad
if(verbose) print("This PAD has two columns, getting a line-chart or column chart or a pie-chart prepared")
if(log.all) logMessage("This PAD has two columns, getting a line-chart or column chart or a pie-chart prepared")
# Initialize
default <- getDefault()
pad.dt <- data.table(pad)
# if both columns are measures, a scatter plot
if((length(pmd$mList) == 2 ) && (length(pmd$dList)==0)) {
if(verbose) print("Both columns are numeric , this is not a time-series pad")
if(log.all) logMessage("Both columns are numeric , this is not a time-series pad")
pad.type <- "other"
## mea - the first numeric column as the measure
default.mea <- names(pmd$mList[which(pmd$mList == "numeric")])[1]
default <- setDefaultMea(default, default.mea)
#set yaxis title
default <- setYaxisTitle(default, default.mea)
# default measure data
default.mea.data <- as.vector(as.character(pad.dt[,get(default.mea)]))
## dim - the second numeric column as the dim
default.dim <- names(pmd$mList[which(pmd$mList == "numeric")])[2]
#set default dim
default <- setDefaultDim(default, default.dim)
#set xaxis tile and categories
default <- setXaxisTitle(default, default.dim)
default.dim.data <- as.vector(as.character(pad.dt[,get(default.dim)]))
default <- setXaxisCat(default, default.dim.data)
#set chart type & chart title
default <- setChartType(default, "column")
default <- setChartTitle(default,pmd$title)
#set pad type
default <- setPadType(default, pad.type )
#add a series
default.series.data=as.matrix(cbind(default.dim.data, default.mea.data))
dimnames(default.series.data) <- NULL
default <- addSeries(default,default.mea, default.series.data)
#print(default)
#return
return(default)
} else if((length(pmd$mList) == 0 ) && (length(pmd$dList)==2)) {
# if both columns are dims - don't know what to do
if(verbose) print("Both columns are non-numeric , this is not a time-series pad")
if(log.all) logMessage("Both columns are non-numeric , this is not a time-series pad")
#
#
#
# To be implemented
#
#
#
#
return(default)
} else if((length(pmd$mList) > 0) && (length(pmd$dList) > 0)) {
if(verbose) print("There is at least one dim and one measure, this is a ok pad")
if(log.all) logMessage("There is at lease one dim and one measure, this is a ok pad")
## mea - the first numeric column as the measure
default.mea <- names(pmd$mList[which(pmd$mList == "numeric")])[1]
if(!is.null(default.mea) && !is.na(default.mea)) {
default <- setDefaultMea(default, default.mea)
#set yaxis title
default <- setYaxisTitle(default, default.mea)
# default measure data
default.mea.data = as.vector(as.character(pad.dt[,get(default.mea)]))
#
# Get the first dim of type date
#
default.dim <- names(pmd$dList[which(pmd$dList == "Date")])[1]
if(is.null(default.dim) || is.na(default.dim)) {
# date is not a dimension in this pad
if(verbose) print("This is not a time-series pad")
if(log.all) logMessage("This is not a time-series pad")
# Setting the pad type
pad.type <- "other"
#
# dim of type date doesn't exist, finding factor
#
if(verbose) print("dim of type date doesn't exist, finding factor")
if(log.all) logMessage("dim of type date doesn't exist, finding factor")
tem <- names(pmd$dList[which(pmd$dList == "character")])
if(!is.null(tem) && !is.na(tem) && (length(tem) > 0)) {
#check which dim can be of type factors
tem2 = tem[which(length(as.factor(pad[,tem])) != length(pad[,tem]))]
if(!is.null(tem2) && !is.na(tem2) && (length(tem2) > 0)) {
#if dims can be coverted to factor, use the first dim as default
default.dim = tem2[1]
if(verbose) print("Found a dim which is a factor")
if(log.all) logMessage("Found a dim which is a factor")
default.dim.data = as.character(pad.dt[,get(default.dim)])
default.series.data=as.character(pad.dt[,sum(get(default.mea), na.rm=TRUE),
by=get(default.dim)])
default.chart.type="column"
default.xaxis.categories <- levels(as.factor(pad.dt[,get(default.dim)]))
} else {
#same as scatter plot when using date as default dim
if(verbose) print("Found a dim which is a character")
if(log.all) logMessage("Found a dim which is a character")
default.dim = tem[1]
#print(pad.dt)
default.dim.data = as.character(pad.dt[,get(default.dim)])
#print(default.dim.data)
default.series.data <- cbind(as.character(pad.dt[,get(default.dim)]), as.character(pad.dt[,get(default.mea)]) )
#print(default.series.data)
dimnames(default.series.data) <- NULL
default.chart.type <- "bar"
default.xaxis.categories = as.character(pad.dt[,get(default.dim)])
}
#set default dim
default <- setDefaultDim(default, default.dim)
#set xaxis tile and categories
default <- setXaxisTitle(default, default.dim)
default <- setXaxisCat(default, default.xaxis.categories)
#set chart type & chart title
default <- setChartType(default, default.chart.type)
default <- setChartTitle(default, pmd$title)
#set pad type
default <- setPadType(default, pad.type)
#add a series
dimnames(default.series.data) <- NULL
default <- addSeries(default,default.mea, default.series.data)
#print(default)
#return
return(default)
} else {
if(verbose) print("dim is null: no dimensions found, bad pad")
if(log.all) logMessage("dim is null: no dimensions found, bad pad")
#pmd <- c(pmd,type=pad.type)
}
} else { # if dim of date exists
if(verbose) print("Found a dim which is a date")
if(log.all) logMessage("Found a dim which is a date")
pad.type <- "timeseries"
default.dim.data = as.character(pad.dt[,get(default.dim)])
#set default dim
default <- setDefaultDim(default, default.dim)
#set xaxis tile and categories
default <- setXaxisTitle(default, default.dim)
default <- setXaxisCat(default, default.dim.data)
#set chart type & chart title
default <- setChartType(default, "line")
default <- setChartTitle(default, pmd$title)
#set pad type
default <- setPadType(default, pad.type )
#add a series
default.series.data <- as.matrix(cbind(as.character(pad.dt[,get(default.dim)]), as.character(pad.dt[,get(default.mea)]) ))
dimnames(default.series.data) <- NULL
default <- addSeries(default,default.mea, default.series.data)
#print(default)
#return
return(default)
}
}
}
}
#
# get chart for a pad containing three columns
#
getChartForThreeColPad <- function(pad, pmd) {
if(verbose) print("This PAD has three columns, sending it to chartForThreeColPad to get a line-chart or column chart or a pie-chart prepared")
if(log.all) logMessage("This PAD has more than three columns, sending it to chartForThreeColPad to get a line-chart or column chart or a pie-chart prepared")
# 3 column in this pad
# Get the first mea of type numeric
# Initialize
default <- getDefault()
pad.dt <- data.table(pad)
#assign("pad.dt", pad.dt, envir=.GlobalEnv)
#assign("pmd", pmd, envir=.GlobalEnv)
# if two columns are measures and the other one is a dim
if((length(pmd$mList) == 2 ) && (length(pmd$dList)==1)) {
## need to plot two line charts
if(verbose) print("Two measures and one dimension in this pad")
if(log.all) logMessage("Two measures and one dimension in this pad")
pad.type <- "other"
## dim - the only column as the dim - check if it is date
default.dim <- names(pmd$dList[which(pmd$dList == "Date")])[1]
if(!is.null(default.dim) && !is.na(default.dim)) {
pad.type <- "timeseries"
default.chart.type="line"
} else { #assuming character vecotr
default.dim = names(pmd$dList[which(pmd$dList == "character")])[1]
pad.type <- "other"
default.chart.type="column"
}
## mea - the first numeric column as the measure
default.mea <- names(pmd$mList[which(pmd$mList == "numeric")])[1]
default <- setDefaultMea(default, default.mea)
# default measure data
default.mea.data = as.vector(as.character(pad.dt[,get(default.mea)]))
## mea - the second numeric column as the measure
default.mea.second = names(pmd$mList[which(pmd$mList == "numeric")])[2]
default.mea.second.data = as.vector(as.character(pad.dt[,get(default.mea.second)]))
default.yaxis.second.title=default.mea.second
#set yaxis title
default <- setYaxisTitle(default, paste(default.mea, default.mea.second, sep="-"))
#set chart type & chart title
default <- setChartType(default, default.chart.type)
default <- setChartTitle(default,pmd$title)
#set default dim
default <- setDefaultDim(default, default.dim)
#set xaxis tile and categories
default <- setXaxisTitle(default, default.dim)
default.dim.data <- as.vector(as.character(pad.dt[,get(default.dim)]))
default <- setXaxisCat(default, default.dim.data)
#set pad type
default <- setPadType(default, pad.type )
#add a series
default.series.data=as.matrix(cbind(default.dim.data, default.mea.data))
dimnames(default.series.data) <- NULL
default <- addSeries(default,default.mea, default.series.data)
#print(default)
## add a second sereis
default.series.second.data=as.matrix(cbind(default.dim.data, default.mea.second.data))
dimnames(default.series.second.data) <- NULL
default <- addSeries(default,default.mea.second, default.series.second.data)
#print(default)
#return
return(default)
} else if((length(pmd$mList) == 3 ) && (length(pmd$dList)==0)) {
#
# All three columns are numeric, this is not a time-series pad, preparing for bubble chart
#
if(verbose) print("All three columns are numeric, this is not a time-series pad, preparing for bubble chart")
if(log.all) logMessage("All three columns are numeric, this is not a time-series pad, preparing for bubble chart")
## mea - the first numeric column as the measure
default.mea = names(pmd$mList[which(pmd$mList == "numeric")])[1]
default <- setDefaultMea(default, default.mea)
#set yaxis title
default <- setYaxisTitle(default, default.mea)
# default measure data
default.mea.data = as.character(pad.dt[,get(default.mea)])
## dim - the second numeric column as the dim
default.dim = names(pmd$mList[which(pmd$mList == "numeric")])[2]
#set default dim
default <- setDefaultDim(default, default.dim)
#set xaxis tile and categories
default <- setXaxisTitle(default, default.dim)
default.dim.data = as.character(pad.dt[,get(default.dim)])
default <- setXaxisCat(default, default.dim.data)
## size - the third numeric column as the size
default.size = names(pmd$mList[which(pmd$mList == "numeric")])[3]
default.size.data = as.character(pad.dt[,get(default.size)])
default <- setSizeTitle(default, default.size)
#set chart type & chart title
default <- setChartType(default, "bubble")
default <- setChartTitle(default,pmd$title)
#prepare a series
default.series.data=as.matrix(cbind(default.dim.data, default.mea.data, default.size.data))
dimnames(default.series.data) <- NULL
#add a series
default <- addSeries(default, default.size, default.series.data)
#return
return(default)
} else if((length(pmd$mList) == 1 ) && (length(pmd$dList)==2)) {
# if(length(levels(as.factor(pad.dt[,get(default.dim)]))) != length(pad.dt[,get(default.dim)]))
if(verbose) print("Two columns are non-numeric i.e. dimensions ")
if(log.all) logMessage("Two columns are non-numeric i.e. dimensions")
pad.type <- "other"
## dim - the only column as the dim - check if it is date
default.dim <- names(pmd$dList[which(pmd$dList == "Date")])[1]
if(!is.null(default.dim) && !is.na(default.dim)) {
pad.type <- "timeseries"
default.chart.type="line"
} else { #assuming character vecotr
default.dim = names(pmd$dList[which(pmd$dList == "character")])[1]
pad.type <- "other"
default.chart.type="bar"
}
#set pad type
default <- setPadType(default, pad.type )
#set chart type & chart title
default <- setChartType(default, default.chart.type)
default <- setChartTitle(default,pmd$title)
## mea - the first numeric column as the measure
default.mea <- names(pmd$mList[which(pmd$mList == "numeric")])[1]
default <- setDefaultMea(default, default.mea)
#set yaxis title
default <- setYaxisTitle(default, default.mea)
# default measure data
default.mea.data = as.vector(as.character(pad.dt[,get(default.mea)]))
## dim - take the second dim as the x-axis
## The best thing to do will be a to find which of the two dim columns are factor
#set xaxis tile and categories
default <- setXaxisTitle(default, default.dim)
default.dim.data <- as.vector(as.character(pad.dt[,get(default.dim)]))
default.dim.factor <- F
#check for factor
if(length(levels(as.factor(pad.dt[,get(default.dim)]))) != length(pad.dt[,get(default.dim)])){
if(verbose) print(paste(default.dim, " is a factor", sep=""))
default.dim.factor <- T
default.dim.data <- as.vector(levels(as.factor(pad.dt[,get(default.dim)])))
}
#lapply(tem, function(x) length(levels(as.factor(pad.dt[,get(x)]))) != length(pad.dt[,get(x)]))
#check whether second dim is also a factor
default.dim.second = names(pmd$dList[which(pmd$dList == "character")])[2]
default.dim.second.data <- as.vector(as.character(pad.dt[,get(default.dim.second)]))
default.dim.second.factor <- F
if(length(levels(as.factor(pad.dt[,get(default.dim.second)]))) != length(pad.dt[,get(default.dim.second)])){
#print(paste(default.dim.second, " is a factor", sep=""))
default.dim.second.factor <- T
default.dim.second.data <- as.vector(levels(as.factor(pad.dt[,get(default.dim.second)])))
}
if(default.dim.factor && default.dim.second.factor) {
#add a series
setkeyv(pad.dt, default.dim.second)
for(i in default.dim.second.data){
series.name = i
#print(series.name)
data = as.vector(as.character(pad.dt[series.name][, get(default.mea)]))
series.data=as.matrix(cbind(default.dim.data, data))
dimnames(series.data) <- NULL
default <- addSeries(default,series.name, series.data)
}
default <- setDefaultDim(default, default.dim)
default <- setXaxisCat(default, default.dim.data)
default <- setChartTypeStacked(default)
} else if (default.dim.factor && !default.dim.second.factor) {
default.series.data=as.matrix(cbind(default.dim.second.data, default.mea.data))
dimnames(default.series.data) <- NULL
default <- addSeries(default,default.mea, default.series.data)
default <- setDefaultDim(default, default.dim.second)
default <- setXaxisCat(default, default.dim.second.data)
# use first dim as special labels
} else if (!default.dim.factor && default.dim.second.factor) {
default.series.data=as.matrix(cbind(default.dim.data, default.mea.data))
dimnames(default.series.data) <- NULL
default <- addSeries(default,default.mea, default.series.data)
default <- setDefaultDim(default, default.dim)
# use first dim as special labels
}
#print(default)
return(default)
} else if((length(pmd$mList) == 0 ) && (length(pmd$dList)==3)) {
# if both columns are dims - don't know what to do
if(verbose) print("All three columns are dimensions, don't know what to do")
if(log.all) logMessage("All three columns are dimensions, don't know what to do")
}
}
#
# get chart for a pad containing Four columns
#
getChartForFourColPad <- function(pad, pmd) {
if(verbose) print("This PAD has four columns...")
if(log.all) logMessage("This PAD has four columns...")
# 4 column in this pad
# Get the first mea of type numeric
# Initialize
default <- getDefault()
pad.dt <- data.table(pad)
# if three columns are measures and the other one is a dim
if((length(pmd$mList) == 3 ) && (length(pmd$dList)==1)) {
if(verbose) print("Three measures and one dimension in this pad")
if(log.all) logMessage("Three measures and one dimension in this pad")
pad.type <- "other"
## mea - the first numeric column as the measure
default.mea <- names(pmd$mList[which(pmd$mList == "numeric")])[1]
default <- setDefaultMea(default, default.mea)
#set yaxis title
default <- setYaxisTitle(default, default.mea)
# default measure data
default.mea.data = as.vector(as.character(pad.dt[,get(default.mea)]))
## mea - the second numeric column as the measure
default.mea.second = names(pmd$mList[which(pmd$mList == "numeric")])[2]
default.mea.second.data = as.vector(as.character(pad.dt[,get(default.mea.second)]))
default.yaxis.second.title=default.mea.second
## mea - the third numeric column as the measure
default.mea.3rd = names(pmd$mList[which(pmd$mList == "numeric")])[3]
default.mea.3rd.data = as.vector(as.character(pad.dt[,get(default.mea.3rd)]))
default.yaxis.3rd.title=default.mea.3rd
## dim - the only column as the dim - check if it is date
default.dim <- names(pmd$dList[which(pmd$dList == "Date")])[1]
if(!is.null(default.dim) && !is.na(default.dim)) {
pad.type <- "timeseries"
default.chart.type="line"
} else { #assuming character vecotr
default.dim = names(pmd$dList[which(pmd$dList == "character")])[1]
pad.type <- "other"
default.chart.type="bar"
}
#set chart type & chart title
default <- setChartType(default, default.chart.type)
default <- setChartTitle(default,pmd$title)
#set default dim
default <- setDefaultDim(default, default.dim)
#set xaxis tile and categories
default <- setXaxisTitle(default, default.dim)
default.dim.data <- as.vector(as.character(pad.dt[,get(default.dim)]))
default <- setXaxisCat(default, default.dim.data)
#set pad type
default <- setPadType(default, pad.type )
#add a series
default.series.data=as.matrix(cbind(default.dim.data, default.mea.data))
dimnames(default.series.data) <- NULL
default <- addSeries(default,default.mea, default.series.data)
#print(default)
## add a second sereis
default.series.second.data=as.matrix(cbind(default.dim.data, default.mea.second.data))
dimnames(default.series.second.data) <- NULL
default <- addSeries(default,default.mea.second, default.series.second.data)
## add a third sereis
default.series.3rd.data=as.matrix(cbind(default.dim.data, default.mea.3rd.data))
dimnames(default.series.3rd.data) <- NULL
default <- addSeries(default,default.mea.3rd, default.series.3rd.data)
#return
return(default)
}
}
getChartForMultiColPad <- function(pad, pmd) {
if(verbose) print("This PAD has many columns...")
if(log.all) logMessage("This PAD has many columns...")
# Initialize
default <- getDefault()
pad.dt <- data.table(pad)
# if three columns are measures and the other one is a dim
if((length(pmd$dList)==1)) {
if(verbose) print("One dimension in this pad, assuming rest are measures")
if(log.all) logMessage("One dimension in this pad, assuming rest are measures")
## dim - the only column as the dim - check if it is date
default.dim <- names(pmd$dList[which(pmd$dList == "Date")])[1]
if(!is.null(default.dim) && !is.na(default.dim)) {
pad.type <- "timeseries"
default.chart.type="line"
} else { #assuming character vecotr
default.dim = names(pmd$dList[which(pmd$dList == "character")])[1]
pad.type <- "other"
default.chart.type="bar"
}
#set chart type & chart title
default <- setChartType(default, default.chart.type)
default <- setChartTitle(default,pmd$title)
#set default dim
default <- setDefaultDim(default, default.dim)
#set xaxis tile and categories
default <- setXaxisTitle(default, default.dim)
default.dim.data <- as.vector(as.character(pad.dt[,get(default.dim)]))
default <- setXaxisCat(default, default.dim.data)
#set pad type
default <- setPadType(default, pad.type )
## mea - the first numeric column as the measure
default.mea <- names(pmd$mList[which(pmd$mList == "numeric")])[1]
default <- setDefaultMea(default, default.mea)
#set yaxis title
default <- setYaxisTitle(default, default.mea)
#add a series
for(i in 1:length(pmd$mList)){
series.name = names(pmd$mList[which(pmd$mList == "numeric")])[i]
data = as.vector(as.character(pad.dt[,get(series.name)]))
series.data=as.matrix(cbind(default.dim.data, data))
dimnames(series.data) <- NULL
default <- addSeries(default,series.name, series.data)
}
#return
return(default)
}
}
#
# prepare default chart for a pad
#
getDefaultChartInfo <- function(pad, pmd) {#pmd is pad meta data
if(verbose) print("Starting the default chart identification process")
#converting the pad data.frame to a data.table
pad.dt <- data.table(pad)
#check the column count in the pad
# Continue if there is atleast one column
l <- NULL
if(pmd$columns > 4 ) {
if(verbose) print("This PAD has more than four columns, sending it to chartForTwoColPad to get a line-chart or column chart or a pie-chart prepared")
if(log.all) logMessage("This PAD has more than four columns, sending it to chartForTwoColPad to get a line-chart or column chart or a pie-chart prepared")
l <- getChartForMultiColPad(pad, pmd)
} else {
l <- switch(pmd$columns,
getChartForSingleColPad(pad, pmd),
getChartForTwoColPad(pad, pmd),
getChartForThreeColPad(pad, pmd),
getChartForFourColPad(pad, pmd)
)
}
#print(l)
if(!is.null(l))
pmd <- c(pmd, type=l$type, default=list(l))
return(pmd)
}