Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Ginza.js #1 LT_Draw_LineChart

Ginza.js #1 LT_Draw_LineChart

Ginza.js #1 LT用資料
タイトル「JavaScriptを好きになるためにLineChartを描画してみた」

MzRyuKa

May 22, 2019
Tweet

More Decks by MzRyuKa

Other Decks in Technology

Transcript

  1. ࣗݾ঺հɿΈͣΓΎ { ৬ۀ: ‘ಠཱܥSIer’, ΤϯδχΞྺ: ‘20೥໨ʹಥೖ’, ࢓ࣄͰ࢖͏ٕज़: [ ‘Ruby’,’RRDtool’,’SNMP’,’JavaScript’ ],

    ਪ͠ݴޠ: [‘Golang’,’Elixir’], ޷͖ͳ΋ͷ: [ ‘Ͷ͜’, ’͏͞·Δ’,’ٕज़࿩’], Twitter : { TwitterID: ‘@MzRyuKa’, ঢ়ଶ: 'Search Suggestion Ban’ }, ϒϩά: ‘https://mzryuka.hatenablog.jp/’ } !2
  2. ༨ஊɿόΠΦϦζϜͱ͸ ੜ໋ମͷʮ਎ମʢPhysicalʣʯʮײ৘ ʢSensitivityʣʯʮ஌ੑʢIntellectualʣʯ͕Ұ ఆͷपظύλʔϯͰมԽ͢Δͱݴ͏આʹجͮ ͍ͨάϥϑͷ͜ͱ ʮਖ਼ݭ೾ʯͷ೾ܗͰɺy = sin(2 π t

    / T) 
 ͷࣜͰࢉग़͞ΕΔɻ t: ج४೔ - ੜ೥݄೔ ͔Βࢉग़͞ΕΔ೔਺ T: ਎ମʢPʣͷ৔߹23ɺײ৘ʢPʣͷ৔߹28ɺ ஌ੑʢIʣͷ৔߹33ͷݻఆͷ஋ͱͳΔɻ 23ͱ28ͱ33ͷ࠷খެഒ਺͸21252Ͱ͋Δɻ
 ೔਺ʹ௚͢ͱʮ58.2೥ʯͰ͋Δɻ !12 //Create biorhythm data const [p_v, s_v, i_v] = (function() { const pi = Math.PI const biorhythm_func = (function(T) { return Math.sin(elapsed_days * 2 * pi / T).toFixed(4) }) const physical = biorhythm_func(23); const sensitivity = biorhythm_func(28); const intellectual = biorhythm_func(33); return [physical, sensitivity, intellectual] })();
  3. Chart.js ར༻ྫ1 !15 var ctx = document.getElementById('myChart_b'); var myChart =

    new Chart(ctx, { type: 'line', data: { datasets: [{ label: '# of Physical', fill: false, data: [], backgroundColor: 'rgba(0, 0, 255, 0.3)', borderColor: 'rgba(0, 0, 255, 0.3)', borderWidth: 3 } ,{ label: '# of Sensitivity', fill: false, data: [], backgroundColor: 'rgba(255, 0, 0, 0.3)', ~ҎԼུ~ document.getElementById()Ͱඳըର৅ʹ͢ΔCanvasλάΛ ࢦఆ͢Δɻ canvasΦϒδΣΫτΛChartΦϒδΣΫτͷҾ਺ʹ౉͢Δ ͜ͱͰؔ࿈͚ͮΔɻ·ͨɺChartͷઃఆ͸ΦϒδΣΫτܗ ࣜͰ౉͢ɻ νϟʔτͷछྨ͸type:Ͱࢦఆ͢Δɻ’line’ʹ͢ΔͱʮϥΠ ϯνϟʔτʯʹͳΔɻ σʔλ͸ɺdata.datasets ʹରͯ͠ɺΦϒδΣΫτͷ഑ྻ Λ౉͢ɻΦϒδΣΫτ͸߲໨਺෼༻ҙ͢Δɻ ֤߲໨ͷΦϒδΣΫτ಺ͷʮdataʯʹදࣔ͢ΔσʔλΛ ઃఆ͢Δɻ
  4. Chart.js ར༻ྫ2 !16 options: { scales: { xAxes: [{ type:

    'time', distribution: 'series', time: { unit: 'day', displayFormats: {'day': 'YYYY-MM-DD'} }, scaleLabel: { display: true, labelString: '೔࣌' } }], yAxes: [{ ticks: { } }] } } ~ҎԼུ~ optionsΦϒδΣΫτΛ౉͢͜ͱͰɺX࣠΍Y࣠ ʹରͯ͠ͷઃఆ΋ՄೳʹͳΔɻ X࣠Λ࣌ܥྻʹ͢ΔͨΊʹɺtypeͰ’time’Λɺ distributionͰ’series’Λࢦఆ͢Δɻ X࣠ͷ஋Λ೔෇ܗࣜYYYY-MM-DDͰઃఆ͢Δɻ X࣠ʹࢦఆ͢ΔϥϕϧΛ’೔࣌’ʹ͢Δɻ
  5. C3.js ར༻ྫ1 !18 <html> <head> <meta charset="UTF-8"> <link href="https://cdnjs.cloudflare.com/ajax/ libs/c3/0.7.0/c3.min.css"

    rel="stylesheet" type="text/css"> <script src="https://cdnjs.cloudflare.com/ajax/ libs/d3/5.9.2/d3.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/ libs/c3/0.7.0/c3.min.js"></script> <title>c3.jsʹΑΔνϟʔτදࣔ</title> </head> <body> <div id="chart"></div> <script> var chart = c3.generate({ bindto: '#chart', ~ҎԼུ~ D3.jsΛར༻͢ΔͨΊɺD3.jsͷࢦఆ͕ඞཁʹͳ Δɻ c3.js͸ɺಉ࣌ʹc3.cssͷࢦఆ΋ඞཁʹͳΔɻ divλάͰඳը͢ΔՕॴΛઃఆɻ c3.generateͰɺchartΦϒδΣΫτΛੜ੒
  6. C3.js ར༻ྫ2 !19 grid: { x: { lines: [ {value:

    moment().format('YYYY-MM-DD'), text: 'ج४೔', position: 'start'}, ] }, y: { show: true, lines: [ {value: 0.0, text: 'ෆ҆ఆ', axis: 'y', position:’start'}, {value: 1.0, text: 'ߴௐ', axis: 'y', position: 'start'}, {value: -1.0, text: '௿ௐ', axis: 'y', position: 'start'}, ] } }, // animation: tansition duration: miliseconds transition: { duration: 5000 }, // sub chart(Experimental) subchart: {show: true} ~ҎԼུ~ X࣠ʹิॿઢΛೖΕΔ৔߹ɺgrid.x.linesʹର͠ ͯઃఆͷΦϒδΣΫτͷ഑ྻΛೖΕΔɻ Y࣠ʹิॿઢΛೖΕΔ৔߹ɺgrid.x.linesʹର͠ ͯઃఆͷΦϒδΣΫτͷ഑ྻΛೖΕΔɻ transition.duration Ͱɺνϟʔτͷඳը࣌ͷΞ χϝʔγϣϯॲཧ͕࣌ؒઃఆͰ͖Δɻ ࣮ݧػೳ͕ͩɺαϒνϟʔτ΋ࢦఆͰ͖Δɻ
  7. Google Chart ར༻ྫ1 !21 const data = new google.visualization.DataTable(); data.addColumn('string',

    'X'); data.addColumn('number', 'physical'); data.addColumn('number', 'sensitivity'); data.addColumn('number', 'intellectual'); data.addRows(chartData); const options = { 'title': 'Biorhythm Line Chart', 'width':900, 'height':500, hAxis: { title: 'Date', gridlines: {color: '#333', count: 10} }, vAxis: { title: 'Biorhythm PSI', gridlines: {color: '#333', count: 10} }, }; ~ҎԼུ~ දࣔσʔλ͸google.visualization.DatatTableͷΦ ϒδΣΫτΛੜ੒ͯ͠ɺͦ͜ʹઃఆ͢Δɻ දࣔ͢Δ߲໨͸addColumnͰݸʑʹ௥Ճ͢Δɻ දࣔσʔλ͸addRowsͰઃఆ͢Δɻઃఆσʔλ ͸ɺ[ [1,2,3,4], [5,6,7,8], [~ུ~], ... ]ͷΑ͏ʹೋ࣍ ݩ഑ྻɻʢ಺෦ͷ഑ྻཁૉ਺͸ɺ߲໨਺ʹ߹Θ ͤΔɻࠨͷྫͩͱ4ͭʣ දࣔσʔλҎ֎ͷઃఆ৘ใʢνϟʔτͷαΠζ ΍X࣠Y࣠ͷ৘ใͳͲʣ͸ɺΦϒδΣΫτʹઃఆ ͢Δɻ
  8. Google Chart ར༻ྫ2 !22 const chart = new google.visualization.LineChart(document.getElementById('chart_div')); //

    printing png : // https://developers.google.com/chart/interactive/docs/printing const my_chart_div = document.getElementById('linechart_png'); google.visualization.events.addListener(chart, 'ready', function () { my_chart_div.innerHTML = '<img src="' + chart.getImageURI() + '">'; }); chart.draw(data, options); google.visualizetion.LineChartͰϥΠϯνϟʔτͷΦ ϒδΣΫτΛੜ੒ getImageURIϝιουΛར༻͢Ε͹ɺνϟʔτͷΠ ϝʔδਤ΋औಘՄೳɻ ϥΠϯνϟʔτʹΠϕϯτ΋௥ՃՄೳ ϕʔλ൛ͰʮϚςϦΞϧ൛ͷϥΠϯνϟʔτʯ΋ ར༻Ͱ͖ΔɻʢϚ΢εΦʔόʔ࣌ͷॲཧͳͲ͕߽ ՚ʹͳ͍ͬͯΔɻʣ ϚςϦΞϧ༻ͷϥΠϯνϟʔτʹ͢Δʹ͸ɺ packageͰlineΛϩʔυɺgoogle.charts.LineΦϒδΣ Ϋτੜ੒ɺϚςϦΞϧ༻ͷoptionsͷม׵͕ඞཁɻ google.charts.load('current', {packages: ['corechart', 'line']}); ɹɹɹɹɹɹ~தུ~ const chart = new google.charts.Line(document.getElementById('linechart_material')); chart.draw(data, google.charts.Line.convertOptions(options)); ɹɹɹɹɹɹ~ҎԼུ~