Skip to content

Commit

Permalink
add totals
Browse files Browse the repository at this point in the history
  • Loading branch information
ronzim committed Oct 3, 2022
1 parent 2cf2dc1 commit 712329a
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 6 deletions.
7 changes: 4 additions & 3 deletions src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@
<v-icon class="mr-2">mdi-poll</v-icon>
Chart
</v-btn>
<v-btn to="/totals">
<v-icon class="mr-2">mdi-poll</v-icon>
Totals
</v-btn>
<!-- <v-btn>
<v-icon>mdi-format-align-right</v-icon>
</v-btn>
Expand Down Expand Up @@ -263,7 +267,6 @@ export default Vue.extend({
setTimeout(() => {
// it seems that the div is not ready (mounted ?) try with next tick
console.log("cat", this.categories);
if (this.allDataReady) {
this.computeAndRender();
} else {
Expand Down Expand Up @@ -306,11 +309,9 @@ export default Vue.extend({
// this.$router.push("chart");
},
getColor: function (categoryName: string) {
console.log("cat", categoryName);
let categoryObj = this.categories
.filter(c => c.name == categoryName)
.pop();
console.log("obj", categoryObj ? categoryObj.color : "?");
return categoryObj ? categoryObj.color : "gray";
},
Expand Down
48 changes: 45 additions & 3 deletions src/api.charts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,15 +182,57 @@ export function drawLineChart(expenses: any, getColor: (a: string) => string) {
}))
.filter((ld: any) => ld.value);

console.log(labelsData);

Plotly.newPlot("chart", chartData.concat(labelsData), layout, {
responsive: true
});

const graphDiv = <PlotHTMLElement>document.getElementById("chart")!;
graphDiv.on("plotly_selected", function (eventData: any) {
console.log(eventData);
console.warn(
"total selected",
_.sum(eventData.points.map((p: any) => p.value))
);
});
}

export function drawTotalsChart(
expenses: any,
getColor: (a: string) => string
) {
console.log(expenses);

// const chartData: Plotly.BarData[] = [
const chartData: any = {
type: "bar",
x: expenses.map(exp => exp.category),
y: expenses.map(exp => exp.value),
name: expenses.map(exp => exp.category),
marker: {
color: expenses.map(exp => getColor(exp.category))
}
};

const layout: Partial<Plotly.Layout> = {
height: 600,
title: "Sum by category",
paper_bgcolor: "rgba(0,0,0,0)",
plot_bgcolor: "rgba(0,0,0,0)",
font: {
family: "Avenir, Helvetica, Arial, sans-serif",
size: 14,
color: "#afafaf"
}
// barmode: "stack"
};

console.log(chartData);

Plotly.newPlot("chart2", [chartData], layout, {
responsive: true
});

const graphDiv = <PlotHTMLElement>document.getElementById("chart2")!;
graphDiv.on("plotly_selected", function (eventData: any) {
console.warn(
"total selected",
_.sum(eventData.points.map((p: any) => p.value))
Expand Down
7 changes: 7 additions & 0 deletions src/router/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import Vue from "vue";
import VueRouter, { RouteConfig } from "vue-router";
import Table from "@/views/Table.vue";
import Chart from "@/views/Chart.vue";
import Chart2 from "@/views/Chart2.vue";

Vue.use(VueRouter);

Expand All @@ -19,6 +20,12 @@ const routes: Array<RouteConfig> = [
// which is lazy-loaded when the route is visited.
component: () =>
import(/* webpackChunkName: "about" */ "../views/Chart.vue")
},
{
path: "/totals",
name: "Totals",
component: () =>
import(/* webpackChunkName: "about" */ "../views/Chart2.vue")
}
];

Expand Down
40 changes: 40 additions & 0 deletions src/views/Chart2.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<template>
<v-row>
<v-col>
<v-card>
<v-card-text>
<div id="chart2"></div>
</v-card-text>
</v-card>
</v-col>
</v-row>
</template>

<script lang="ts">
import Vue, { PropType } from "vue";
import { Category } from "../types";
import { drawTotalsChart } from "../api.charts";
export default Vue.extend({
name: "Table",
data: () => ({
search: "" as string
}),
props: {
jsonData: { type: [] as PropType<any[]>, required: true },
expenses: { type: [] as PropType<any[]>, required: true },
categories: { type: [] as PropType<Category[]>, required: true },
getColorFn: {
type: [] as PropType<(arg0: string) => string>,
required: true
}
},
mounted: function () {
drawTotalsChart(this.expenses, this.getColorFn);
},
computed: {},
methods: {}
});
</script>

0 comments on commit 712329a

Please sign in to comment.