Yu Wen Wang
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Make a copy
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Make a copy Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    ###### tags: `JS 學徒特訓班` # JS 學徒特訓班 30 C3.js 圖表整合筆記 題目:https://hackmd.io/Vp1FRGPMRymM7FOxaQ_srQ ### 載入js、css、外部資料 - [d3.js](https://cdnjs.cloudflare.com/ajax/libs/d3/5.16.0/d3.min.js) - [c3.js](https://c3js.org/examples.html) - [長條圖](https://c3js.org/samples/chart_bar.html) - [c3 的 css](https://cdnjs.cloudflare.com/ajax/libs/c3/0.7.18/c3.min.css) - [axios](https://github.com/axios/axios) - [練習API](https://raw.githubusercontent.com/hexschool/hexschoolNewbieJS/master/data.json) ### 定義資料與取得資料 ```javascript= var url = 'https://raw.githubusercontent.com/hexschool/hexschoolNewbieJS/master/data.json'; var data = []; axios.get(url) .then(function(res){ data = res.data; setData(data);//跑排序 render(columns,namelist);//進行圖表化 }) ``` 因為 c3.js 的資料呈現方式為 ```javascript= columns: [ ["標題", 資料] ], ``` 原始取得的資料呈現為 ```javascript= 0: {id: "1", name: "廖洧杰(測試名稱)", process: "15%", checkpoint: {…}} 1: {id: "2", name: "Kun", process: "10%", checkpoint: {…}} 2: {id: "3", name: "Hung", process: "0%", checkpoint: {…}} ... ``` 所以取得的資料必須重新組合 先將原有資料重新排序之後再取出組合 ```javascript= //增加定義的資料 let columns = ["完成率"];//參賽者完成率 let namelist = [];//參賽者姓名 //排序資料與取出資料放到新定義的陣列 function setData(data){ data.sort((a,b) => parseInt(b.process) - parseInt(a.process)); data.forEach((item,index)=>{ columns.push(item.process);//完成率陣列 namelist.push(item.name); }); } ``` 接下來進行 c3.js 資料視覺化 先在 html 裡面增加一個 div ```javascript= <div id="chart"></div> ``` 進行 c3.js 的資料建構(直接複製範本過來修改) ```javascript= function render(columns,namelist){ var chart = c3.generate({ data: { columns: [ [...columns],//將前面重新組成的完成率放入 ], type: 'bar',//使用的圖表類型 colors: { 完成率: "#9CCC65", //這是指每一個陣列資料的顏色,而此處只有一個完成率的陣列 //["完成率", 45%, 45% ....] //因此前面使用陣列的第一個 "完成率",後面指定顏色 }, axes: {//旋轉之後完成率會出現在最下方,所以在上方再新增另一個 Y 軸 完成率: 'y2' } }, bar: { width: { ratio: 0.1 //設定 bar 的寬度 } }, size: { height: namelist.length * 30, //調整圖表高度 }, axios: { //用來設定 x,y 軸 //上面的姓名資料會出現在 X 軸,但是總比數非常多,所以旋轉圖表 rotated: true, //但是旋轉之後完成率會出現在最下方,所以在上方 data 要再新增另一個 Y 軸 x: { type: "category", //這裡是指用下方的 categories 來顯示 X 軸,也就是參賽者名稱 //只能使用 category 不能用其他字代替 //我在這邊卡好久才搞懂 //參考: https://c3js.org/samples/categorized.html categories: namelist, }, y: { show: true, //下方 Y 軸顯示 label: { text: "完成率%", position: "outer-center", //名稱位置 }, }, y2: { show: true, //上方 Y 軸顯示 label: { text: "完成率%", position: "outer-center", //名稱位置 }, } } }); } ``` - [data.colors](https://c3js.org/reference.html#data-colors) - [增加另一個 Y 軸](https://c3js.org/reference.html#data-axes) - [X Y 軸的設定](https://c3js.org/reference.html#axis-rotated) - [axios.x.categories](https://c3js.org/samples/categorized.html) - [axios 基本使用 & Config](https://ithelp.ithome.com.tw/articles/10212120)

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully