forked from pyrochlore/obsidian-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpie.ts
More file actions
169 lines (146 loc) · 4.25 KB
/
pie.ts
File metadata and controls
169 lines (146 loc) · 4.25 KB
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
import { Moment } from "moment";
import {
Datasets,
DataPoint,
RenderInfo,
PieInfo,
MonthInfo,
Dataset,
Size,
Transform,
ChartElements,
OutputType,
ValueType,
} from "./data";
import * as helper from "./helper";
import * as d3 from "d3";
import * as expr from "./expr";
function createAreas(
chartElements: ChartElements,
canvas: HTMLElement,
renderInfo: RenderInfo,
pieInfo: PieInfo
): ChartElements {
// clean areas
d3.select(canvas).select("#svg").remove();
var props = Object.getOwnPropertyNames(chartElements);
for (var i = 0; i < props.length; i++) {
// d3.select(chartElements[props[i]]).remove();
delete chartElements[props[i]];
}
// console.log(chartElements);
// whole area for plotting, includes margins
let svg = d3
.select(canvas)
.append("svg")
.attr("id", "svg")
.attr(
"width",
renderInfo.dataAreaSize.width +
renderInfo.margin.left +
renderInfo.margin.right
)
.attr(
"height",
renderInfo.dataAreaSize.height +
renderInfo.margin.top +
renderInfo.margin.bottom
);
chartElements["svg"] = svg;
// graphArea, includes chartArea, title, legend
let graphArea = svg
.append("g")
.attr("id", "graphArea")
.attr(
"transform",
"translate(" +
renderInfo.margin.left +
"," +
renderInfo.margin.top +
")"
)
.attr("width", renderInfo.dataAreaSize.width + renderInfo.margin.right)
.attr(
"height",
renderInfo.dataAreaSize.height + renderInfo.margin.bottom
);
chartElements["graphArea"] = graphArea;
// dataArea, under graphArea, includes points, lines, xAxis, yAxis
let dataArea = graphArea
.append("g")
.attr("id", "dataArea")
.attr("width", renderInfo.dataAreaSize.width)
.attr("height", renderInfo.dataAreaSize.height);
chartElements["dataArea"] = dataArea;
return chartElements;
}
function renderTitle(
canvas: HTMLElement,
chartElements: ChartElements,
renderInfo: RenderInfo,
pieInfo: PieInfo
) {
// console.log("renderTitle");
// under graphArea
if (!renderInfo || !pieInfo) return;
if (!pieInfo.title) return;
let titleSize = helper.measureTextSize(pieInfo.title, "tracker-title");
}
function renderPie(
canvas: HTMLElement,
chartElements: ChartElements,
renderInfo: RenderInfo,
pieInfo: PieInfo
) {
// console.log("renderPie");
// console.log(renderInfo);
let radius = renderInfo.dataAreaSize.width * 0.5 * 0.8;
// data
let data = pieInfo.data.map(function (s) {
let value = expr.resolve(s, renderInfo);
return value;
});
// scale
let colorScale = d3.scaleOrdinal().range(pieInfo.dataColor);
let sectorsGroup = chartElements.dataArea.append("g");
sectorsGroup.attr("transform", function () {
let strTranslate =
"translate(" +
renderInfo.dataAreaSize.width * 0.5 +
"," +
renderInfo.dataAreaSize.height * 0.5 +
")";
return strTranslate;
});
let pie = d3.pie();
let sectors = sectorsGroup
.selectAll("sector")
.data(pie(data))
.enter()
.append("g")
.attr("class", "sector");
let arc = d3
.arc()
.innerRadius(radius * pieInfo.ratioInnerRadius)
.outerRadius(radius);
let sectorPaths = sectors
.append("path")
.attr("fill", function (d: any, i: number) {
return colorScale(i.toString());
})
.attr("d", arc);
}
export function renderPieChart(
canvas: HTMLElement,
renderInfo: RenderInfo,
pieInfo: PieInfo
) {
// console.log("renderPieChart");
// console.log(renderInfo);
if (!renderInfo || !pieInfo) return;
return "Under construction";
let chartElements: ChartElements = {};
chartElements = createAreas(chartElements, canvas, renderInfo, pieInfo);
renderTitle(canvas, chartElements, renderInfo, pieInfo);
renderPie(canvas, chartElements, renderInfo, pieInfo);
}