$value ) {
if ( $value <= 0 ) {
continue;
}
$len = ( $value / $total ) * $circumference;
$color = self::PALETTE[ $i % count( self::PALETTE ) ];
$segments .= sprintf(
'%8$s: %9$s',
esc_attr( $c ),
esc_attr( $r ),
esc_attr( $color ),
esc_attr( $stroke ),
esc_attr( round( $len, 3 ) ),
esc_attr( round( $circumference - $len, 3 ) ),
esc_attr( round( -$offset, 3 ) ),
esc_html( $label ),
esc_html( number_format_i18n( $value ) )
);
$offset += $len;
$i++;
}
return sprintf(
'',
$size,
esc_attr__( 'Distribution chart', 'argus-wordpress-defence' ),
esc_attr( $c ),
esc_attr( $r ),
esc_attr( $stroke ),
$segments,
esc_html( number_format_i18n( $total ) )
);
}
public static function legend( array $data ) {
$total = array_sum( $data );
if ( $total <= 0 ) {
return '';
}
$html = '
';
$i = 0;
foreach ( $data as $label => $value ) {
if ( $value <= 0 ) {
continue;
}
$color = self::PALETTE[ $i % count( self::PALETTE ) ];
$pct = round( $value / $total * 100 );
$html .= sprintf(
'
%2$s%3$s (%4$s%%)
',
esc_attr( $color ),
esc_html( $label ),
esc_html( number_format_i18n( $value ) ),
esc_html( $pct )
);
$i++;
}
$html .= '
';
return $html;
}
public static function area_chart( array $series, $width = 640, $height = 160 ) {
$count = count( $series );
if ( $count < 2 ) {
return '';
}
$values = wp_list_pluck( $series, 'value' );
$max = max( 1, max( $values ) );
$pad_x = 8;
$pad_y = 10;
$plot_w = $width - ( $pad_x * 2 );
$plot_h = $height - ( $pad_y * 2 );
$step = $plot_w / ( $count - 1 );
$points = array();
foreach ( array_values( $values ) as $i => $value ) {
$x = $pad_x + ( $i * $step );
$y = $pad_y + $plot_h - ( ( $value / $max ) * $plot_h );
$points[] = array( round( $x, 2 ), round( $y, 2 ) );
}
$line_path = 'M ' . implode( ' L ', array_map( function ( $p ) {
return $p[0] . ' ' . $p[1];
}, $points ) );
$fill_path = $line_path
. sprintf( ' L %s %s', round( $pad_x + $plot_w, 2 ), round( $pad_y + $plot_h, 2 ) )
. sprintf( ' L %s %s Z', $pad_x, round( $pad_y + $plot_h, 2 ) );
$last = end( $points );
return sprintf(
'',
$width,
$height,
esc_attr__( 'Blocked requests over time', 'argus-wordpress-defence' ),
esc_attr( $fill_path ),
esc_attr( $line_path ),
esc_attr( $last[0] ),
esc_attr( $last[1] )
);
}
}