Skip to content

Commit

Permalink
Refactor: Fix chart change animation
Browse files Browse the repository at this point in the history
  • Loading branch information
rarestoma committed Feb 22, 2021
1 parent 725c8e3 commit 2fe3779
Show file tree
Hide file tree
Showing 2 changed files with 137 additions and 87 deletions.
82 changes: 1 addition & 81 deletions src/components/Charts/Chart.js
Original file line number Diff line number Diff line change
@@ -1,84 +1,5 @@
import Chart from "chart.js";

export const salesChart = {
createChart(chartId, chartData) {
const chartColor = "#FFFFFF";
const fallBackColor = "#f96332";
const color = this.color || fallBackColor;
const ctx = document.getElementById(chartId).getContext("2d");
const gradientStroke = ctx.createLinearGradient(500, 0, 100, 0);
gradientStroke.addColorStop(0, color);
gradientStroke.addColorStop(1, chartColor);

new Chart(ctx, {
type: "line",
data: {
labels: ["May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"],
datasets: [
{
label: "Performance",
tension: 0.4,
borderWidth: 4,
borderColor: "#5e72e4",
pointRadius: 0,
backgroundColor: "transparent",
data: chartData,
},
],
},
options: {
responsive: true,
maintainAspectRatio: false,
legend: {
display: false,
},
tooltips: {
enabled: true,
mode: "index",
intersect: false,
},
scales: {
yAxes: [
{
barPercentage: 1.6,
gridLines: {
drawBorder: false,
color: "rgba(29,140,248,0.0)",
zeroLineColor: "transparent",
},
ticks: {
padding: 0,
fontColor: "#8898aa",
fontSize: 13,
fontFamily: "Open Sans",
},
},
],
xAxes: [
{
barPercentage: 1.6,
gridLines: {
drawBorder: false,
color: "rgba(29,140,248,0.0)",
zeroLineColor: "transparent",
},
ticks: {
padding: 10,
fontColor: "#8898aa",
fontSize: 13,
fontFamily: "Open Sans",
},
},
],
},
layout: {
padding: 0,
},
},
});
},
};

export const ordersChart = {
createChart(chartId) {
const chartColor = "#FFFFFF";
Expand Down Expand Up @@ -165,8 +86,7 @@ export const ordersChart = {
};

const funcs = {
salesChart() {},
ordersChart() {},
ordersChart() {}
};

export default funcs;
142 changes: 136 additions & 6 deletions src/views/Dashboard.vue
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,12 @@
</template>
<script>
// Charts
import { salesChart, ordersChart } from "@/components/Charts/Chart";
import { ordersChart } from "@/components/Charts/Chart";
import Chart from "chart.js";
import PageVisitsTable from "./Dashboard/PageVisitsTable";
import SocialTrafficTable from "./Dashboard/SocialTrafficTable";
let chart;
export default {
components: {
Expand All @@ -174,15 +176,143 @@ export default {
},
methods: {
initBigChart(index) {
salesChart.createChart(
this.salesChartID,
this.bigLineChart.allData[index]
);
chart.destroy();
chart = new Chart(document.getElementById(this.salesChartID).getContext("2d"), {
type: "line",
data: {
labels: ["May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"],
datasets: [
{
label: "Performance",
tension: 0.4,
borderWidth: 4,
borderColor: "#5e72e4",
pointRadius: 0,
backgroundColor: "transparent",
data: this.bigLineChart.allData[index],
},
],
},
options: {
responsive: true,
maintainAspectRatio: false,
legend: {
display: false,
},
tooltips: {
enabled: true,
mode: "index",
intersect: false,
},
scales: {
yAxes: [
{
barPercentage: 1.6,
gridLines: {
drawBorder: false,
color: "rgba(29,140,248,0.0)",
zeroLineColor: "transparent",
},
ticks: {
padding: 0,
fontColor: "#8898aa",
fontSize: 13,
fontFamily: "Open Sans",
},
},
],
xAxes: [
{
barPercentage: 1.6,
gridLines: {
drawBorder: false,
color: "rgba(29,140,248,0.0)",
zeroLineColor: "transparent",
},
ticks: {
padding: 10,
fontColor: "#8898aa",
fontSize: 13,
fontFamily: "Open Sans",
},
},
],
},
layout: {
padding: 0,
},
},
});
this.bigLineChart.activeIndex = index;
},
},
mounted() {
salesChart.createChart(this.salesChartID, this.bigLineChart.allData[0]);
chart = new Chart(document.getElementById(this.salesChartID).getContext("2d"), {
type: "line",
data: {
labels: ["May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"],
datasets: [
{
label: "Performance",
tension: 0.4,
borderWidth: 4,
borderColor: "#5e72e4",
pointRadius: 0,
backgroundColor: "transparent",
data: this.bigLineChart.allData[1],
},
],
},
options: {
responsive: true,
maintainAspectRatio: false,
legend: {
display: false,
},
tooltips: {
enabled: true,
mode: "index",
intersect: false,
},
scales: {
yAxes: [
{
barPercentage: 1.6,
gridLines: {
drawBorder: false,
color: "rgba(29,140,248,0.0)",
zeroLineColor: "transparent",
},
ticks: {
padding: 0,
fontColor: "#8898aa",
fontSize: 13,
fontFamily: "Open Sans",
},
},
],
xAxes: [
{
barPercentage: 1.6,
gridLines: {
drawBorder: false,
color: "rgba(29,140,248,0.0)",
zeroLineColor: "transparent",
},
ticks: {
padding: 10,
fontColor: "#8898aa",
fontSize: 13,
fontFamily: "Open Sans",
},
},
],
},
layout: {
padding: 0,
},
},
});
ordersChart.createChart(this.ordersChartID);
},
};
Expand Down

0 comments on commit 2fe3779

Please sign in to comment.