Skip to content

Commit

Permalink
feat: chart.js
Browse files Browse the repository at this point in the history
  • Loading branch information
Renovamen committed Jan 31, 2021
1 parent 2075d79 commit 8a0a9c3
Show file tree
Hide file tree
Showing 13 changed files with 331 additions and 5 deletions.
1 change: 1 addition & 0 deletions example/.vuepress/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ module.exports = {
}
],
'@renovamen/vuepress-plugin-katex',
'@renovamen/vuepress-plugin-chart'
],
markdown: {
// lineNumbers: true,
Expand Down
45 changes: 45 additions & 0 deletions example/posts/2020-10-29-hello-word.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,51 @@ while True:
me.eat(10)
```


## Charts

### Chart.js

```chart
{
"type": "bar",
"data": {
"labels": ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
"datasets": [{
"label": "# of Votes",
"data": [12, 19, 3, 5, 2, 3],
"backgroundColor": [
"rgba(255, 99, 132, 0.2)",
"rgba(54, 162, 235, 0.2)",
"rgba(255, 206, 86, 0.2)",
"rgba(75, 192, 192, 0.2)",
"rgba(153, 102, 255, 0.2)",
"rgba(255, 159, 64, 0.2)"
],
"borderColor": [
"rgba(255, 99, 132, 1)",
"rgba(54, 162, 235, 1)",
"rgba(255, 206, 86, 1)",
"rgba(75, 192, 192, 1)",
"rgba(153, 102, 255, 1)",
"rgba(255, 159, 64, 1)"
],
"borderWidth": 1
}]
},
"options": {
"scales": {
"yAxes": [{
"ticks": {
"beginAtZero": true
}
}]
}
}
}
```


## Images

![Image Example](/img/home-bg/3.jpg)
Expand Down
45 changes: 45 additions & 0 deletions example/posts/2020-10-30-hello-word-with-header-image.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,51 @@ while True:
me.eat(10)
```


## Charts

### Chart.js

```chart
{
"type": "bar",
"data": {
"labels": ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
"datasets": [{
"label": "# of Votes",
"data": [12, 19, 3, 5, 2, 3],
"backgroundColor": [
"rgba(255, 99, 132, 0.2)",
"rgba(54, 162, 235, 0.2)",
"rgba(255, 206, 86, 0.2)",
"rgba(75, 192, 192, 0.2)",
"rgba(153, 102, 255, 0.2)",
"rgba(255, 159, 64, 0.2)"
],
"borderColor": [
"rgba(255, 99, 132, 1)",
"rgba(54, 162, 235, 1)",
"rgba(255, 206, 86, 1)",
"rgba(75, 192, 192, 1)",
"rgba(153, 102, 255, 1)",
"rgba(255, 159, 64, 1)"
],
"borderWidth": 1
}]
},
"options": {
"scales": {
"yAxes": [{
"ticks": {
"beginAtZero": true
}
}]
}
}
}
```


## Images

![Image Example](/img/home-bg/3.jpg)
Expand Down
2 changes: 1 addition & 1 deletion packages/@renovamen/vuepress-plugin-baidu-tongji/LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ THE SOFTWARE.

-----------------------------------------------------------------------------

@renovamen/vuepress-plugin-google-analytics is using the part of the following
@renovamen/vuepress-plugin-baidu-tongji is using the part of the following
codes with the following licenses:

MIT License
Expand Down
31 changes: 31 additions & 0 deletions packages/@renovamen/vuepress-plugin-chart/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
MIT License

Copyright (c) 2021 Xiaohan Zou (@Renovamen)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

-----------------------------------------------------------------------------

@renovamen/vuepress-plugin-chart is using the part of the following codes with
the following licenses:

MIT License
Copyright (c) 2016 Waylon Flinn
markdown-it-katex: https://github.com/waylonflinn/markdown-it-katex
License: https://github.com/waylonflinn/markdown-it-katex/blob/master/LICENSE
75 changes: 75 additions & 0 deletions packages/@renovamen/vuepress-plugin-chart/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# @renovamen/vuepress-plugin-chart

A plugin for adding [Chart.js](https://www.chartjs.org) to VuePress.

 

## Usage

Install:

```bash
yarn add @renovamen/vuepress-plugin-chart
# or
npm install @renovamen/vuepress-plugin-chart
```

Add this plugin to your `.vuepress/config.js`:

```js
module.exports = {
plugins: [
[
'@renovamen/vuepress-plugin-chart'
]
]
}
```

Then you can use [Chart.js](https://www.chartjs.org) in Markdown:

<pre><code class="json">```chart
{
"type": "bar",
"data": {
"labels": ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
"datasets": [{
"label": "# of Votes",
"data": [12, 19, 3, 5, 2, 3],
"backgroundColor": [
"rgba(255, 99, 132, 0.2)",
"rgba(54, 162, 235, 0.2)",
"rgba(255, 206, 86, 0.2)",
"rgba(75, 192, 192, 0.2)",
"rgba(153, 102, 255, 0.2)",
"rgba(255, 159, 64, 0.2)"
],
"borderColor": [
"rgba(255, 99, 132, 1)",
"rgba(54, 162, 235, 1)",
"rgba(255, 206, 86, 1)",
"rgba(75, 192, 192, 1)",
"rgba(153, 102, 255, 1)",
"rgba(255, 159, 64, 1)"
],
"borderWidth": 1
}]
},
"options": {
"scales": {
"yAxes": [{
"ticks": {
"beginAtZero": true
}
}]
}
}
}
```
</code></pre>

&nbsp;

## License

[MIT](LICENSE)
6 changes: 6 additions & 0 deletions packages/@renovamen/vuepress-plugin-chart/enhanceApp.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import Chart from './lib/Chart';

export default function (ctx) {
const { Vue } = ctx;
Vue.component('Chart', Chart);
}
14 changes: 14 additions & 0 deletions packages/@renovamen/vuepress-plugin-chart/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const path = require('path')
const chart = require('./lib/markdown-it-chart')

module.exports = (options, ctx) => {
return {
name:'@renovamen/vuepress-plugin-chart',
enhanceAppFiles: path.resolve(__dirname, './enhanceApp.js'),
chainMarkdown(config) {
config
.plugin('chart')
.use(chart)
}
}
}
29 changes: 29 additions & 0 deletions packages/@renovamen/vuepress-plugin-chart/lib/Chart.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<template>
<div class="vuepress-chartjs">
<canvas ref="chartjs"></canvas>
</div>
</template>

<script>
import Chart from 'chart.js'
export default {
name: 'chart',
props: {
code: {
type: String,
required: true
}
},
mounted () {
const data = JSON.parse(this.code);
const ctx = this.$refs.chartjs.getContext('2d');
new Chart(ctx, data);
}
}
</script>

<style lang="stylus">
.vuepress-chartjs
margin 30px 0
</style>
24 changes: 24 additions & 0 deletions packages/@renovamen/vuepress-plugin-chart/lib/markdown-it-chart.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const { hash } = require('@vuepress/shared-utils')

const chart = (md) => {
const temp = md.renderer.rules.fence.bind(md.renderer.rules);
md.renderer.rules.fence = (tokens, idx, options, env, slf) => {
const token = tokens[idx];

if (token.info === 'chart') {
try {
const key = `chart_${hash(idx)}`;
const code = token.content.trim();

md.$dataBlock[key] = code;
return `<Chart :code="$dataBlock.${key}"></Chart>`;
}
catch (err) {
return `<pre>${err}</pre>`
}
}
return temp(tokens, idx, options, env, slf);
}
}

module.exports = chart;
27 changes: 27 additions & 0 deletions packages/@renovamen/vuepress-plugin-chart/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"name": "@renovamen/vuepress-plugin-chart",
"version": "0.1.0",
"description": "A plugin for adding Chart.js to VuePress.",
"author": "Renovamen <renovamenzxh@gmail.com>",
"main": "index.js",
"keywords": [
"vue",
"vuepress",
"vuepress-plugin",
"chart",
"chartjs"
],
"homepage": "https://github.com/Renovamen/vuepress-theme-gungnir/tree/main/packages/@renovamen/vuepress-plugin-chart",
"bugs": {
"url": "https://github.com/Renovamen/vuepress-theme-gungnir/issues"
},
"repository": {
"type": "git",
"url": "git+https://github.com/Renovamen/vuepress-theme-gungnir.git",
"directory": "packages/@renovamen/vuepress-plugin-chart"
},
"license": "MIT",
"dependencies": {
"chart.js": "^2.9.4"
}
}
1 change: 1 addition & 0 deletions packages/theme-gungnir/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
},
"dependencies": {
"oh-vue-icons": "^0.1.7",
"@renovamen/vuepress-plugin-chart": "^0.1.0",
"@renovamen/vuepress-plugin-katex": "^0.1.0",
"@renovamen/vuepress-plugin-baidu-tongji": "^0.1.0",
"@renovamen/vuepress-plugin-md-plus": "^0.1.0",
Expand Down

1 comment on commit 8a0a9c3

@vercel
Copy link

@vercel vercel bot commented on 8a0a9c3 Jan 31, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.