-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathCandleRatings.groovy
83 lines (72 loc) · 3.67 KB
/
CandleRatings.groovy
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
/*
* SPDX-License-Identifier: Apache-2.0
*
* 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
*
* https://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.
*/
import tech.tablesaw.api.*
import tech.tablesaw.io.xlsx.XlsxReader
import tech.tablesaw.plotly.components.*
import tech.tablesaw.plotly.traces.ScatterTrace
import tech.tablesaw.plotly.traces.Trace
import tech.tablesaw.selection.Selection
import java.time.*
import java.util.function.Function
import static java.time.Month.JANUARY
import static tech.tablesaw.aggregate.AggregateFunctions.mean
import static tech.tablesaw.api.QuerySupport.and
import static tech.tablesaw.io.xlsx.XlsxReadOptions.builder
// helper function
List<Trace> traces(URL url, String lineColor, String markerColor) {
def table = new XlsxReader().read(builder(url).build())
table.addColumns(
DateColumn.create('YearMonth', table.column('Date').collect { LocalDate.of(it.year, it.month, 15) })
)
def janFirst2017 = LocalDateTime.of(2017, JANUARY, 1, 0, 0)
Function<Table, Selection> from2017 = r -> r.dateTimeColumn('Date').isAfter(janFirst2017)
Function<Table, Selection> top3 = r -> r.intColumn('CandleID').isLessThanOrEqualTo(3)
def byMonth = table.sortAscendingOn('Date')
.where(and(from2017, top3))
.summarize('Rating', mean).by('YearMonth')
def byDate = table.sortAscendingOn('Date')
.where(and(from2017, top3))
.summarize('Rating', mean).by('Date')
def averaged = ScatterTrace.builder(byMonth.dateColumn('YearMonth'), byMonth.nCol('Mean [Rating]'))
.mode(ScatterTrace.Mode.LINE)
.line(Line.builder().width(5).color(lineColor).shape(Line.Shape.SPLINE).smoothing(1.3).build())
.build()
def scatter = ScatterTrace.builder(byDate.dateTimeColumn('Date'), byDate.nCol('Mean [Rating]'))
.marker(Marker.builder().opacity(0.3).color(markerColor).build())
.build()
[averaged, scatter]
}
Layout layout(String variant) {
Layout.builder("Top 3 $variant candles Amazon reviews 2017-2020", 'Date', 'Average daily rating (1-5)')
.showLegend(false).width(1000).height(500).build()
}
// create the start of COVID line
def covidReported = LocalDateTime.of(2020, JANUARY, 20, 0, 0)
def reported = Table.create(DateTimeColumn.create('Date'), IntColumn.create('Val'))
reported.appendRow().with {setDateTime('Date', covidReported); setInt('Val', 1) }
reported.appendRow().with {setDateTime('Date', covidReported); setInt('Val', 5) }
def line = ScatterTrace.builder(reported.dateTimeColumn('Date'), reported.nCol('Val'))
.mode(ScatterTrace.Mode.LINE)
.line(Line.builder().width(2).dash(Line.Dash.DOT).color('red').build())
.build()
def url = getClass().classLoader.getResource('Scented_all.xlsx')
def helper = new TablesawHelper(url.file)
def (sAverage, sScatter) = traces(url, 'seablue', 'lightskyblue')
url = getClass().classLoader.getResource('Unscented_all.xlsx')
def (uAverage, uScatter) = traces(url, 'seagreen', 'lightgreen')
//helper.show(new Figure(layout(''), sAverage, sScatter, uAverage, uScatter, line))
helper.show(new Figure(layout('scented'), sAverage, sScatter, line), 'ScentedRatings')
helper.show(new Figure(layout('unscented'), uAverage, uScatter, line), 'UnscentedRatings')