Skip to content

Commit

Permalink
fix: some sonar issues
Browse files Browse the repository at this point in the history
  • Loading branch information
kurkle committed Oct 26, 2024
1 parent 3b485da commit e2b071f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 16 deletions.
23 changes: 10 additions & 13 deletions src/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,9 @@ function getAddY(arr: FromToElement[], key: string, index: number): number {
}

export default class SankeyController extends DatasetController {
static id = 'sankey'
static readonly id = 'sankey'

static defaults = {
static readonly defaults = {
dataElementType: 'flow',
animations: {
numbers: {
Expand Down Expand Up @@ -122,7 +122,7 @@ export default class SankeyController extends DatasetController {
},
}

static overrides = {
static readonly overrides = {
interaction: {
mode: 'nearest',
intersect: true,
Expand Down Expand Up @@ -289,8 +289,7 @@ export default class SankeyController extends DatasetController {
)
}

// updateSharedOptions typings are wrong in Chart.js, it accepts undefined also
this.updateSharedOptions(sharedOptions!, mode, firstOpts)
this.updateSharedOptions(sharedOptions, mode, firstOpts)
}

private _drawLabels() {

Check warning on line 295 in src/controller.ts

View workflow job for this annotation

GitHub Actions / ci

Method '_drawLabels' has a complexity of 13. Maximum allowed is 10
Expand All @@ -308,13 +307,12 @@ export default class SankeyController extends DatasetController {
ctx.save()
const chartArea = this.chart.chartArea
for (const node of nodes.values()) {
// Assuming all nodes have x & y values here
const x = xScale.getPixelForValue(node.x!)
const y = yScale.getPixelForValue(node.y!)
const x = xScale.getPixelForValue(node.x)
const y = yScale.getPixelForValue(node.y)

const max = Math[size](node.in || node.out, node.out || node.in)
const height = Math.abs(yScale.getPixelForValue(node.y! + max) - y)
const label = (labels && labels[node.key]) || node.key
const label = labels?.[node.key] ?? node.key
let textX = x
ctx.fillStyle = options.color ?? 'black'
ctx.textBaseline = 'middle'
Expand All @@ -331,8 +329,7 @@ export default class SankeyController extends DatasetController {
}

private _drawLabel(label: string, y: number, height: number, ctx: CanvasRenderingContext2D, textX: number) {
// Probably another typing issue in Chart.js with toFont
const font = toFont(this.options.font!, this.chart.options.font)
const font = toFont(this.options.font, this.chart.options.font)
const lines = toTextLines(label)
const lineCount = lines.length
const middle = y + height / 2
Expand Down Expand Up @@ -366,8 +363,8 @@ export default class SankeyController extends DatasetController {

for (const node of nodes.values()) {
ctx.fillStyle = node.color ?? 'black'
const x = xScale!.getPixelForValue(node.x!)
const y = yScale!.getPixelForValue(node.y!)
const x = xScale!.getPixelForValue(node.x)
const y = yScale!.getPixelForValue(node.y)

const max = Math[sizeMethod](node.in || node.out, node.out || node.in)
const height = Math.abs(yScale!.getPixelForValue(node.y! + max) - y)
Expand Down
6 changes: 3 additions & 3 deletions src/flow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ function setStyle(ctx: CanvasRenderingContext2D, { x, x2, options }: FlowConfig)
}

export default class Flow extends Element<FlowProps, FlowOptions> {
static id = 'flow'
static override defaults = {
static readonly id = 'flow'
static override readonly defaults = {
colorFrom: 'red',
colorTo: 'green',
colorMode: 'gradient',
Expand All @@ -54,7 +54,7 @@ export default class Flow extends Element<FlowProps, FlowOptions> {
hoverColorTo: (_ctx, options) => getHoverColor(options.colorTo),
}

static descriptors = {
static readonly descriptors = {
_scriptable: true,
}

Expand Down

0 comments on commit e2b071f

Please sign in to comment.