スパークラインの設定

スパークラインを作成した後は、表示するデータ点の値(最大値、最小値、先頭値、最終値、負の値など)、スパークラインタイプ(折れ線、縦棒、勝敗)の変更、スタイルの適用、水平軸の表示/非表示の切り替えを定義できます。

折れ線スパークラインにおいて、マーカーを表示することで、個々のデータマーカー(値)を強調できます。 showFirst: 始点の書式を設定します。 showHigh: 頂点(山)の書式を設定します。 showLast: 終点の書式を設定します。 showLow: 頂点(谷)の書式を設定します。 showNegative: 負のデータポイントの書式を設定します。 showMarkers: データマーカーの表示状態を設定します。 スパークラインのスタイルおよび書式を変更するには、以下のメソッドを使用します。 axisColor: 軸の色を設定します。 firstMarkerColor: 始点の色を設定します。 highMarkerColor: 頂点(山)の色を設定します。 lastMarkerColor: 終点の色を設定します。 lowMarkerColor: 頂点(谷)の色を設定します。 markersColor: データマーカーの色を示す値を設定します。 negativeColor: 負のデータポイントの色を示す値を設定します。 seriesColor: スパークラインの色を示す値を設定します。 これ以外にも、スパークラインにはさまざまな設定項目があります。たとえば、チャート内のデータ系列に空白値が含まれている場合、displayEmptyCellsAsメソッドを使用すると、空白セルの表示方法を指定できます。次に、例を示します。 次のサンプルコードは、これらの設定の適用方法を示します。
window.onload = function () { var spread = new GC.Spread.Sheets.Workbook(document.getElementById('ss')); initSpread(spread); }; function initSpread(spread) { var spreadNS = GC.Spread.Sheets; var sheet = spread.getSheet(0); sheet.suspendPaint(); sheet.options.allowCellOverflow = true; var data = [1,-2,-1,6,4,-4,3,8]; var dateAxis = [new Date(2011, 0, 5),new Date(2011, 0, 1),new Date(2011, 1, 11),new Date(2011, 2, 1), new Date(2011, 1, 1),new Date(2011, 1, 3),new Date(2011, 2, 6),new Date(2011, 1, 19)]; sheet.setValue(0, 0, "Series 1"); sheet.setValue(0, 1, "Series 2"); for(let i=0;i<8;i++) { sheet.setValue(i+1, 0,data[i]); sheet.getCell(i+1, 1).value(dateAxis[i]).formatter("yyyy-mm-dd"); } sheet.setColumnWidth(1,100); sheet.setValue(10, 0, "*Data Range is A2-A9"); sheet.setValue(11, 0, "*Date axis range is B2-B9"); var dataRange = new spreadNS.Range(1, 0, 8, 1); var dateAxisRange = new spreadNS.Range(1, 1, 8, 1); sheet.getCell(13, 0).text("Sparkline with dateAxis:"); sheet.getCell(14, 0).text("(1) Line"); sheet.getCell(14, 3).text("(2)Column"); sheet.getCell(14, 6).text("(3)Winloss"); //line sheet.addSpan(15, 0, 4, 3); var setting = new spreadNS.Sparklines.SparklineSetting(); setting.options.showMarkers = true; setting.options.displayXAxis = true; setting.options.showFirst = true; setting.options.showLast = true; setting.options.showLow = true; setting.options.showHigh = true; setting.options.showNegative = true; sheet.setSparkline(15, 0, dataRange , spreadNS.Sparklines.DataOrientation.vertical , spreadNS.Sparklines.SparklineType.line , setting , dateAxisRange , spreadNS.Sparklines.DataOrientation.vertical ); //column sheet.addSpan(15, 3, 4, 3); setting = new spreadNS.Sparklines.SparklineSetting(); setting.options.displayXAxis = true; setting.options.showFirst = true; setting.options.showLast = true; setting.options.showLow = true; setting.options.showHigh = true; setting.options.showNegative = true; sheet.setSparkline(15, 3, dataRange , spreadNS.Sparklines.DataOrientation.vertical , spreadNS.Sparklines.SparklineType.column , setting , dateAxisRange , spreadNS.Sparklines.DataOrientation.vertical ); //winloss sheet.addSpan(15, 6, 4, 3); setting = new spreadNS.Sparklines.SparklineSetting(); setting.options.displayXAxis = true; setting.options.showNegative = true; sheet.setSparkline(15, 6, dataRange , spreadNS.Sparklines.DataOrientation.vertical , spreadNS.Sparklines.SparklineType.winloss , setting , dateAxisRange , spreadNS.Sparklines.DataOrientation.vertical ); sheet.bind(spreadNS.Events.SelectionChanged, selectionChangedCallback); sheet.resumePaint(); function selectionChangedCallback() { var sheet = spread.getActiveSheet(); var sparkline = sheet.getSparkline(sheet.getActiveRowIndex(), sheet.getActiveColumnIndex()); if (sparkline) { getSparklineSettings(sparkline); _getElementById('btnChangeSetting').disabled = false; } else { initSparklineSettings(); _getElementById('btnChangeSetting').disabled = true; } } function getActualRange(range, maxRowCount, maxColCount) { var row = range.row < 0 ? 0 : range.row; var col = range.col < 0 ? 0 : range.col; var rowCount = range.rowCount < 0 ? maxRowCount : range.rowCount; var colCount = range.colCount < 0 ? maxColCount : range.colCount; return new spreadNS.Range(row, col, rowCount, colCount); } function findSparkLine(sheet, range) { var row = range.row, col = range.col, rowCount = range.rowCount, colCount = range.colCount; for (var i = 0; i < rowCount; i++) { for (var j = 0; j < colCount; j++) { var sparkline = sheet.getSparkline(row + i, col + j); if (sparkline != null) { return sparkline; } } } return null; } _getElementById('btnChangeSetting').addEventListener('click', function () { var sheet = spread.getActiveSheet(); sheet.suspendPaint(); var sels = sheet.getSelections(); var setting = new spreadNS.Sparklines.SparklineSetting(); var sparklinetype = _getElementById("sparklinetype"); var index = sparklinetype.selectedIndex; var sparklineType = parseInt(sparklinetype.options[index].value, 10); if (sels && sels.length > 0) { var sel = getActualRange(sels[0], sheet.getRowCount(), sheet.getColumnCount()); var sparkline = findSparkLine(sheet, sel); if (sparkline != null) { sparkline.setting(buildSparklineSettings(setting)); sparkline.sparklineType(sparklineType); } } sheet.resumePaint(); }); function initSparklineSettings() { var sparklinetype = _getElementById("sparklinetype"); _selectOption(sparklinetype, '0'); _getElementById('firstMarkerColor').setAttribute('value', '(none)'); _getElementById('highMarkerColor').setAttribute('value', 'Blue'); _getElementById('lastMarkerColor').setAttribute('value', '(none)'); _getElementById('lowMarkerColor').setAttribute('value', 'Blue'); _getElementById('negativeMarkerColor').setAttribute('value', 'Brown'); _getElementById('markersColor').setAttribute('value', '(none)'); _getElementById('AxisColor').setAttribute('value', 'Black'); _getElementById('SeriesColor').setAttribute('value', '(none)'); _getElementById('showFirst').checked = false; _getElementById('showHigh').checked = false; _getElementById('showLast').checked = false; _getElementById('showLow').checked = false; _getElementById('showNegative').checked = false; _getElementById('showMarkers').checked = false; _getElementById('displayXAxis').checked = true; } function getSparklineSettings(sparkline) { var setting = sparkline.setting(); var sparklinetype = _getElementById("sparklinetype"); _selectOption(sparklinetype, sparkline.sparklineType() + ""); _getElementById('firstMarkerColor').setAttribute('value', setting.options.firstMarkerColor); _getElementById('highMarkerColor').setAttribute('value', setting.options.highMarkerColor); _getElementById('lastMarkerColor').setAttribute('value', setting.options.lastMarkerColor); _getElementById('lowMarkerColor').setAttribute('value', setting.options.lowMarkerColor); _getElementById('negativeMarkerColor').setAttribute('value', setting.options.negativeColor); _getElementById('markersColor').setAttribute('value', setting.options.markersColor); _getElementById('AxisColor').setAttribute('value', setting.options.axisColor); _getElementById('SeriesColor').setAttribute('value', setting.options.seriesColor); _getElementById('showFirst').checked = setting.options.showFirst; _getElementById('showHigh').checked = setting.options.showHigh; _getElementById('showLast').checked = setting.options.showLast; _getElementById('showLow').checked = setting.options.showLow; _getElementById('showNegative').checked = setting.options.showNegative; _getElementById('showMarkers').checked = setting.options.showMarkers; _getElementById('displayXAxis').checked = setting.options.displayXAxis; } function buildSparklineSettings(setting) { if (setting == null) setting = new spreadNS.Sparklines.SparklineSetting(); var firstMarkerColor = _getElementById('firstMarkerColor'); if (firstMarkerColor.value != '(none)') setting.options.firstMarkerColor = firstMarkerColor.value; var highMarkerColor = _getElementById('highMarkerColor'); if (highMarkerColor.value != '(none)') setting.options.highMarkerColor = highMarkerColor.value; var lastMarkerColor = _getElementById('lastMarkerColor'); if (lastMarkerColor.value != '(none)') setting.options.lastMarkerColor = lastMarkerColor.value; var lowMarkerColor = _getElementById('lowMarkerColor'); if (lowMarkerColor.value != '(none)') setting.options.lowMarkerColor = lowMarkerColor.value; var negativeMarkerColor = _getElementById('negativeMarkerColor'); if (negativeMarkerColor.value != '(none)') setting.options.negativeColor = negativeMarkerColor.value; var markersColor = _getElementById('markersColor'); if (markersColor.value != '(none)') setting.options.markersColor = markersColor.value; var AxisColor = _getElementById('AxisColor'); if (AxisColor.value != '(none)') setting.options.axisColor = AxisColor.value; var SeriesColor = _getElementById('SeriesColor'); if (SeriesColor.value != '(none)') setting.options.seriesColor = SeriesColor.value; setting.options.showFirst = _getElementById('showFirst').checked; setting.options.showHigh = _getElementById('showHigh').checked; setting.options.showLast = _getElementById('showLast').checked; setting.options.showLow = _getElementById('showLow').checked; setting.options.showNegative = _getElementById('showNegative').checked; setting.options.showMarkers = _getElementById('showMarkers').checked; setting.options.displayXAxis = _getElementById('displayXAxis').checked; return setting; } } function _getElementById(id) { return document.getElementById(id); } function _selectOption(select, value) { for (var i = 0; i < select.length; i++) { var op = select.options[i]; if (op.value === value) { op.selected = true; } else { op.selected = false; } } }
<!doctype html> <html style="height:100%;font-size:14px;"> <head> <meta name="spreadjs culture" content="ja-jp" /> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <link rel="stylesheet" type="text/css" href="$DEMOROOT$/ja/purejs/node_modules/@mescius/spread-sheets/styles/gc.spread.sheets.excel2013white.css"> <script src="$DEMOROOT$/ja/purejs/node_modules/@mescius/spread-sheets/dist/gc.spread.sheets.all.min.js" type="text/javascript"></script> <script src="$DEMOROOT$/ja/purejs/node_modules/@mescius/spread-sheets-resources-ja/dist/gc.spread.sheets.resources.ja.min.js" type="text/javascript"></script> <script src="$DEMOROOT$/spread/source/js/license.js" type="text/javascript"></script> <script src="app.js" type="text/javascript"></script> <link rel="stylesheet" type="text/css" href="styles.css"> </head> <body> <div class="sample-tutorial"> <div id="ss" class="sample-spreadsheets"></div> <div class="options-container"> <div class="option-row"> <label>*スプレッドシートからスパークラインを選択して、属性を変更します</label> </div> <div class="option-row"> <div class="option"> <input id='btnChangeSetting' type='button' value='設定を変更する'/> </div> </div> <div class="option-row"> <div class="option"> <input id='showFirst' type='checkbox'/> <label for="showFirst">showFirst</label> </div> <div class="option"> <input id='showHigh' type='checkbox'/> <label for="showHigh">showHigh</label> </div> <div class="option"> <input id='showLast' type='checkbox'/> <label for="showLast">showLast</label> </div> <div class="option"> <input id='showLow' type='checkbox'/> <label for="showLow">showLow</label> </div> <div class="option"> <input id='showNegative' type='checkbox'/> <label for="showNegative">showNegative</label> </div> <div class="option"> <input id='showMarkers' type='checkbox'/> <label for="showMarkers">showMarkers</label> </div> <div class="option"> <input id="displayXAxis" type="checkbox"/> <label for="displayXAxis">displayXAxis</label> </div> </div> <div class="option-row"> <div class="option"> <label for="firstMarkerColor" class="block">最初の値のマーカー色</label> <input id='firstMarkerColor' class="control" value="(none)"/> </div> <div class="option"> <label for="sparklinetype" class="block">種類:</label> <select id="sparklinetype" class="control"> <option value="0">line</option> <option value="1">column</option> <option value="2">winloss</option> </select> </div> <div class="option"> <label for="highMarkerColor" class="block">最大値のマーカー色</label> <input id='highMarkerColor' class="control" value="Blue"/> </div> <div class="option"> <label for="lastMarkerColor" class="block">最後の値のマーカー色</label> <input id='lastMarkerColor' class="control" value="(none)"/> </div> <div class="option"> <label for="lowMarkerColor" class="block">最低値のマーカー色</label> <input id='lowMarkerColor' class="control" value="Blue"/> </div> <div class="option"> <label for="negativeMarkerColor" class="block">負の値のマーカー色</label> <input id='negativeMarkerColor' class="control" value="Brown"/> </div> <div class="option"> <label for="markersColor" class="block">マーカーの色</label> <input id='markersColor' class="control" value="(none)"/> </div> <div class="option"> <label for="AxisColor" class="block">軸の色</label> <input id='AxisColor' class="control" value="Black"/> </div> <div class="option"> <label for="SeriesColor" class="block">系列の色</label> <input id='SeriesColor' class="control" value="(none)"/> </div> </div> </div> </div> </body> </html>
.sample { position: relative; height: 100%; overflow: auto; } .sample::after { display: block; content: ""; clear: both; } .sample-tutorial { position: relative; height: 100%; overflow: hidden; } .sample-spreadsheets { width: calc(100% - 280px); height: 100%; overflow: hidden; float: left; } .options-container { float: right; width: 280px; padding: 12px; height: 100%; box-sizing: border-box; background: #fbfbfb; overflow: auto; } .option-row { font-size: 14px; padding: 5px; margin-top: 10px; } .option { padding-bottom: 6px; } .block { display: block; padding: 6px 0; } .control { padding: 4px 8px; box-sizing: border-box; width: 100%; } body { position: absolute; top: 0; bottom: 0; left: 0; right: 0; }