Highcharts系列线和y轴线无法格式化。

huangapple go评论168阅读模式
英文:

Highcharts series line and yAxis line not able to format

问题

我刚刚看了你提供的代码,这是一个使用HighCharts和JavaScript创建图表的函数。根据你的描述,你遇到了一些问题,无法更改系列的线宽并且无法在图表中添加y轴线条。你尝试了一些方法,包括使用plotOption和重新绘制线条,但是无论如何线条的粗细都没有改变。甚至在添加悬停效果时,除了标记变大之外,没有任何其他变化。

你认为可能是某些代码覆盖了设置或者顺序有误。你希望有人能够帮助你解决这个问题。

根据你提供的代码,我看到你创建了一个名为createChart的函数,它接受一些参数来创建图表。你还有一个名为processData的函数,用于处理数据。

如果你能提供更多关于问题的信息,比如你添加的代码部分以及你期望的结果,我将能够更好地帮助你解决问题。

英文:

I am new to using HighCharts and Javascript. Through tutorials and trial and error I have been able to get a working highcharts. The only problem now is I think I have added in some code that is making it so that I can not change the lineWidth for any of the series and cannot add a yAxis line to my chart. I have tried everything from plotOption, trying to redraw the lines and no matter what nothing on the lines change in thickness. Not even when adding hover the lines does anything happen other than the markers get bigger.

The backend works fine the html works fine. I can dynamically bring in the charts and view them and export them. Just I can not for the life of me get the lineWidth to thicken or get the yAxis to show. I event tried setting the HighChart default settings and nothing changes when it comes to lineWidth or yAxis.

I think something is overwriting the settings or my sequence is wrong. But I have not been able to figure it out. I am hopping some can help.

Here is a picture of my HTML: HTML of working Chart

Here is my code:

  1. function createChart(container, title, data, hasValue2, hasValue3, isPercentage, isReversed) {
  2. const chartId = 'chart_' + container;
  3. console.log('Creating chart with ID:', chartId);
  4. // Call the createChartCard function to wrap the chart in a Bootstrap card
  5. createChartCard(title, chartId);
  6. // Call Highcharts.chart() after appending the chart container to the DOM
  7. console.log('Rendering chart:', chartId);
  8. // Create the Highcharts chart
  9. Highcharts.chart(chartId, {
  10. chart: {
  11. borderColor: '#4572A7',
  12. plotBorderWidth: 2,
  13. //plotBorderColor: '#CCCCCC',
  14. type: 'line',
  15. // width: 400, // Adjust the width as needed
  16. // height: 200, // Adjust the height as needed
  17. },
  18. credits: {
  19. enabled: false
  20. },
  21. title: {
  22. text: title,
  23. style: {
  24. fontSize: '16px', // Set the font size to 10 pixels
  25. },
  26. },
  27. exporting: {
  28. enabled: true, // Enable exporting
  29. chartOptions: {
  30. chart: {
  31. width: 841, // Set the chart width to fit the landscape A4 paper (841mm)
  32. height: 595, // Set the chart height to fit the landscape A4 paper (595mm)
  33. margin: [50, 50, 50, 50], // Set the chart margins (top, right, bottom, left)
  34. },
  35. },
  36. },
  37. legend: {
  38. enabled: hasValue2 || hasValue3, // Show legend if either value2 or value3 is present
  39. itemStyle: {
  40. color: 'transparent', // Make the legend text color transparent
  41. },
  42. layout: 'vertical',
  43. align: 'right',
  44. verticalAlign: 'middle',
  45. labelFormatter: function () {
  46. // Hide legend entry for "Series3" when chart only has "value1" and "value2"
  47. if (!hasValue3 && this.name === 'Series 3') {
  48. return '';
  49. }
  50. return this.name; // Show other legend entries as usual
  51. },
  52. },
  53. xAxis: {
  54. //lineColor: '#FF0000',
  55. lineWidth: 2,
  56. tickLine: 0,
  57. tickLength: 0,
  58. //lineColor: 'transparent',
  59. type: 'datetime',
  60. //title: {
  61. // text: 'Date',
  62. // },
  63. labels: {
  64. rotation: -90,
  65. y: 10,
  66. formatter: function () {
  67. // Custom formatter function to display the date as "dd/m/yyyy" without leading zeros in the year
  68. // const date = new Date(this.value);
  69. //const day = date.getDate();
  70. // const month = date.getMonth() + 1; // Months are zero-based, so we add 1
  71. // const year = date.getFullYear().toString().slice(-2); // Get the last two digits of the year
  72. // Concatenate the formatted date components
  73. //return day + '/' + month + '/' + year;
  74. return Highcharts.dateFormat('%e %b %y', this.value);
  75. },
  76. style: {
  77. fontSize: '12px', // Set the font size to 10 pixels
  78. },
  79. },
  80. //offset: 0, // Set the offset to move the labels closer to the chart
  81. },
  82. yAxis: {
  83. title: {
  84. text: isPercentage ? 'Percentage' : '',
  85. },
  86. reversed: isReversed,
  87. labels: {
  88. x: -10,
  89. formatter: function () {
  90. if (isPercentage) {
  91. return this.value;
  92. } else if (Math.round(this.value) === this.value) {
  93. return this.value; // Display as a whole number if it is an integer
  94. } else {
  95. return this.value.toFixed(2); // Otherwise, display with 2 decimal places
  96. }
  97. if (isReversed) {
  98. return -this.value; // Reverse the axis values
  99. } else {
  100. return this.value;
  101. }
  102. },
  103. style: {
  104. fontSize: '12px', // Set the font size to 10 pixels
  105. },
  106. },
  107. //offset: 0, // Set the offset to move the labels closer to the chart
  108. max: isPercentage ? 100 : null,
  109. //softMin: isPercentage ? 0 : 0, // Set max to 100 if isPercentage is true
  110. },
  111. tooltip: {
  112. formatter: function () {
  113. let tooltip = Highcharts.dateFormat('%Y-%m-%d', this.x) + '<br>';
  114. if (isPercentage) {
  115. tooltip += 'Percentage: ' + (this.y).toFixed(2) + '<br>';
  116. } else if (Math.round(this.y) === this.y) {
  117. tooltip += 'Value: ' + this.y + '<br>';
  118. } else {
  119. tooltip += 'Value: ' + this.y.toFixed(2) + '<br>';
  120. }
  121. if (hasValue2 && this.point.y2 !== undefined) {
  122. if (isPercentage) {
  123. tooltip += 'Percentage 2: ' + (this.point.y2).toFixed(2) + '<br>';
  124. } else if (Math.round(this.point.y2) === this.point.y2) {
  125. tooltip += 'Value 2: ' + this.point.y2 + '<br>';
  126. } else {
  127. tooltip += 'Value 2: ' + this.point.y2.toFixed(2) + '<br>';
  128. }
  129. }
  130. if (hasValue3 && this.point.y3 !== undefined) {
  131. if (isPercentage) {
  132. tooltip += 'Percentage 3: ' + (this.point.y3).toFixed(2) + '<br>';
  133. } else if (Math.round(this.point.y3) === this.point.y3) {
  134. tooltip += 'Value 3: ' + this.point.y3 + '<br>';
  135. } else {
  136. tooltip += 'Value 3: ' + this.point.y3.toFixed(2) + '<br>';
  137. }
  138. }
  139. return tooltip;
  140. },
  141. },
  142. series: [
  143. // Series 1 (value1)
  144. {
  145. name: '',
  146. data: data.map(item => ({
  147. x: Date.parse(item.x),
  148. y: isPercentage ? item.y : parseFloat(item.y),
  149. })),
  150. color: 'black', // Default line color for Value 1
  151. //visible: !hasValue2 && !hasValue3, // Show only if value2 and value3 are false
  152. lineWidth: 4, // Increase the line width for this series
  153. marker: {
  154. enabled: true,
  155. radius: 4, // Increase the marker size for this series
  156. },
  157. },
  158. // Series 2 (value2)
  159. hasValue2
  160. ? {
  161. name: '',
  162. data: data.map((item, index) => ({
  163. x: Date.parse(item.x),
  164. y: item.y2 !== undefined ? (isPercentage ? item.y2 : parseFloat(item.y2)) : null,
  165. //color: index > 0 && item.y2 < data[index - 1].y2 ? 'red' : 'black',
  166. })),
  167. dashStyle: 'dot', // Set dash style for Value 2
  168. showInLegend: hasValue2,
  169. lineWidth: 3, // Set the line width to 3
  170. color: 'black',
  171. marker: {
  172. enabled: true,
  173. radius: 4, // Increase the marker size for this series
  174. symbol: 'circle', // Increase the marker size for this series
  175. },
  176. }
  177. : {},
  178. // Series 3 (value3)
  179. hasValue3
  180. ? {
  181. name: '',
  182. data: data.map(item => ({
  183. x: Date.parse(item.x),
  184. y: item.y3 !== undefined ? (isPercentage ? item.y3 : parseFloat(item.y3)) : null,
  185. })),
  186. color: 'black', // Default line color for Value 3
  187. dashStyle: 'longdash', // Set dash style for Value 3
  188. showInLegend: hasValue3,
  189. lineWidth: 4, // Increase the line width for this series
  190. marker: {
  191. enabled: true,
  192. radius: 4, // Increase the marker size for this series
  193. symbol: 'circle',// Increase the marker size for this series
  194. },
  195. }
  196. : {},
  197. ],
  198. });
  199. }
  200. function createChartCard(title, chartId) {
  201. const cardHtml = `
  202. <div class="col-md-6">
  203. <div class="card h-100 w-100">
  204. <div class="card-body" style="height: 100%;">
  205. <div id="${chartId}"></div>
  206. </div>
  207. <div class="card-footer text-muted"></div>
  208. </div>
  209. </div>
  210. `;
  211. $('#chartContainer').append(cardHtml);
  212. }
  213. function processData(data) {
  214. const chartsData = {};
  215. data.forEach((row) => {
  216. const chartname = row.chartname;
  217. const isPercentage = Boolean(parseInt(row.ispercentage)); // Convert '0' to false, '1' to true
  218. const isReversed = Boolean(parseInt(row.isreversed)); // Convert '0' to false, '1' to true
  219. console.log(row.isreversed);
  220. if (!chartsData[chartname]) {
  221. chartsData[chartname] = {
  222. title: row.user + ' - ' + row.jobname + ' - ' + chartname,
  223. data: [],
  224. hasValue2: false,
  225. hasValue3: false,
  226. isPercentage: isPercentage,
  227. isReversed: isReversed,
  228. };
  229. }
  230. const x = row.wedate;
  231. const y = parseFloat(row.value1);
  232. let dataPoint = { x, y };
  233. if (row.value2 !== null) {
  234. dataPoint.y2 = parseFloat(row.value2);
  235. chartsData[chartname].hasValue2 = true;
  236. }
  237. if (row.value3 !== null) {
  238. dataPoint.y3 = parseFloat(row.value3);
  239. chartsData[chartname].hasValue3 = true;
  240. }
  241. chartsData[chartname].data.push(dataPoint);
  242. chartsData[chartname].isPercentage = isPercentage; // Set isPercentage property for the chart
  243. chartsData[chartname].isReversed = isReversed; // Set isReversed property for the chart
  244. });
  245. return Object.values(chartsData);

答案1

得分: 0

根据您提供的代码和演示,您的项目中加载了Highcharts样式,并且它们覆盖了图表选项。在这种情况下,您需要使用CSS样式来解决这个问题。您可以在以下文章中了解有关Highcharts样式的更多信息:https://www.highcharts.com/docs/chart-design-and-style/style-by-css

  1. .highcharts-yaxis .highcharts-axis-line {
  2. stroke-width: 10;
  3. }

演示:
https://jsfiddle.net/jsfiddle_demo/xg4cv5ak/

英文:

Based on the code and demo you provided, there are Highcharts styles loaded in your project and they override the chart options. In that case, you need to use CSS styling to fix that. You can read more about styles in Highcahrts in the following article: https://www.highcharts.com/docs/chart-design-and-style/style-by-css

  1. .highcharts-yaxis .highcharts-axis-line {
  2. stroke-width: 10;
  3. }

Demo:
https://jsfiddle.net/jsfiddle_demo/xg4cv5ak/

huangapple
  • 本文由 发表于 2023年8月9日 12:16:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/76864538.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定