javascript 如何更改 chartjs 模板上的轴、标题、图例格式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20567420/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 18:47:44  来源:igfitidea点击:

How do I change axis, title, legend formatting on chartjs template

javascriptaxis-labelschart.js

提问by user3089117

still new to javascript and html, I have managed to build a simple calculator that will also graph some of the outputs. To do so I am using the below chartjs template. I would like to be able to change the font for the y-axis, title, and legend, as it looks really 'choppy' when it is created, however, I do not know where I would find/define the appropriate paramaters and what they would be. Also, how would I make the chart title wrap into a second line if it gets to long for one? Can I specify these items in my script or do I have to manipulate the dx.chartjs file, and if so where do I need to make the changes?

我还是 javascript 和 html 的新手,我设法构建了一个简单的计算器,它也可以绘制一些输出的图形。为此,我使用了以下 chartjs 模板。我希望能够更改 y 轴、标题和图例的字体,因为它在创建时看起来非常“断断续续”,但是,我不知道在哪里可以找到/定义适当的参数以及什么他们将是。另外,如果图表标题太长,我如何将图表标题换到第二行?我可以在我的脚本中指定这些项目还是必须操作 dx.chartjs 文件,如果可以,我需要在哪里进行更改?

Thank you so much for your help

非常感谢你的帮助

<html>
<head>
<title> Savings </title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">

    <script src="js/jquery-1.10.2.min.js"></script>
    <script src="js/knockout-3.0.0.js"></script>
    <script src="js/globalize.min.js"></script>
    <script src="js/dx.chartjs.js"></script>
</head>

<script language="javascript">

function btnCalculateTotal_Click()
{

houses = 0;
cars = 0;

houses = Number(frmSimpleCalculator.txtValue1.value);
cars = Number(frmSimpleCalculator.txtValue2.value);
sngTotal = houses + cars
frmSimpleCalculator.txtTotal.value = sngTotal;

test = 200
var dataSource = [{ savings: "", Houses: sngTotal, Cars: 300 },];

$("#chartContainer").dxChart({
dataSource: dataSource,
commonSeriesSettings: {
    argumentField: "savings",
    type: "bar",
    hoverMode: "allArgumentPoints",
    selectionMode: "allArgumentPoints",
    label: {
        visible: true,
        format: "fixedPoint",
        precision: 0
            }
                      },
series: [
    { valueField: "Houses", name: "Houses" },
    { valueField: "Cars", name: "Cars" }
        ],

title: "Savings potential",
legend: {
    verticalAlignment: "bottom",
    horizontalAlignment: "center"
        },

pointClick: function (point) {
    this.select();
                             }
                        });
}


</script>

<body bgcolor="#aaaaaa">

<table style="background-color:black; color:white; width:800">
<tr>
<td>
<strong>calculator</strong>
</td>
</tr>
</table>

<form name="frmSimpleCalculator" action="" method="get">
<fieldset name="fraSimpleCalculator" style="width:1">
<legend>Economic Savings Calculator</legend>
<table width="300" border="0">

<!-- Value 1 -->
<tr>
<td align="left">
Houses:
</td>
<td align="right">
<input type="text" value="50000" name="txtValue1" size="10" maxlength="10" style="text-align:right">
</td>
</tr>

<!-- Value 2 -->
<tr>
<td align="left">
Cars:
</td>
<td align="right">
<input type="text" value="25" name="txtValue2" size="10" maxlength="10" style="text-align:right">
</td>
</tr>

<!-- Horizontal rule -->
<tr>
<td align="center" colspan="2">
<hr />
</td>
</tr>

<!-- Total -->
<tr>
<td align="left">
Total:
</td>
<td align="right">
<input type="text" value="0" name="txtTotal" size="10" maxlength="5" style="text-align:right">
</td>
</tr>

<!-- Calculate Button -->
<tr>
<td align="center" colspan="2">
<input type="button" value="Calculate Total" name="btnCalculateTotal" style="width:150" OnClick="btnCalculateTotal_Click();">
</td>
</tr>

<!-- Calculate Chart Button -->
<tr>
<td align="center" colspan="2">
<input type="button" value="Update Chart" name="btnCalculateChart" style="width:150" OnClick="Thomas();">
</td>
</tr>


    <div class="content">
        <div class="pane">
            <div class="long-title"><h3></h3></div>
            <div id="chartContainer" style="width: 250px; height: 440px;" style="position:absolute; TOP:200px; LEFT:350px"></div>       
        </div>
    </div>
</body>
</html>

回答by Andrey Kuleshov

1) you can set font options for title, legend and axes through "font" property of corresponding options' element

1)您可以通过相应选项元素的“字体”属性设置标题、图例和轴的字体选项

legend: {
   font: {
      color: 'black',
      family: 'Zapf-Chancery, cursive',
      opacity: 1,
      weight: 700
    }
}

You can find more sample in ChartJS documentation.

您可以在ChartJS 文档中找到更多示例。

2) You can use HTML tag brto wrap your title to second line

2) 您可以使用 HTML 标签br将您的标题包装到第二行

title: "This is my<br/>long title"