Git Product home page Git Product logo

ng-knob's Introduction

ng-knob

Join the chat at https://gitter.im/RadMie/ng-knob

Angular.js directive to Knob component powered by d3.js (without jQuery)

screenshot

Features

  • very easy to implement
  • without jQuery dependencies
  • powered by d3.js
  • 2-way data binding
  • configurable minimum, maximum values and step
  • animated
  • shows previous value
  • tiny - 11.5kb minified
  • great ability to configure
  • configurable scale
  • touch, click and drag events implemented

Dependencies

  • AngularJS (tested with 1.4.x although it probably works with older versions)
  • D3.js (tested with 3.5.x although it probably works with older versions)

Browser Support

  • Chrome, Firefox, Safari, Opera, IE9+

Get started

Installation

You can also use bower to install the component:

$ bower install ng-knob --save

Usage

HTML:
<body ng-app="KnobDemoApp">
  <div ng-controller="knobCtrl">
    <ui-knob value="value" options="options"></ui-knob>
  </div>
</body>

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.10/d3.min.js"></script>
<script src="bower_components/ng-knob/dist/ng-knob.min.js"></script>
Angular.js:
var app = angular.module('KnobDemoApp', ['ui.knob'])
app.controller('knobCtrl', function ($scope) {
  $scope.value = 65;
  $scope.options = {
    size: 300
    //other options
  };
});

Options

You can pass these options to the initialize function to set a custom look and feel for the plugin.
Property Type Default Description
skin object { type: 'simple', width: 10, color: 'rgba(255,0,0,.5)', spaceWidth: 5 } Type: simple or tron
animate object { enabled: true, duration: 1000, ease: 'bounce' } Duration in milliseconds, Ease: linear, bounce, sin, cubic, quad, exp, circle
size integer 200 Size of knob in px. It will always be a square
startAngle integer 0 Start angle in degrees
endAngle integer 360 End angle in degrees
unit string '' Unit values
displayInput boolean true Display input value (true or false)
inputFormatter function function(value){ return value; } Formats the input value before appending the unit and displaying it to the DOM
readOnly boolean false Disabled change value (true or false)
trackWidth integer 50 Width track bar in px
barWidth integer 50 Width bar value in px
trackColor string 'rgba(0,0,0,0)' Color track bar
barColor string 'rgba(255,0,0,.5)' Color bar value
prevBarColor string 'rgba(0,0,0,0)' Color bar previous value
textColor string '#222' Text color
barCap integer 0 Defines how the ending of the bar line looks like in radius
trackCap integer 0 Defines how the ending of the track line looks like in radius
fontSize string 'auto' Font size in px. auto: automatic change
subText object { enabled: false, text: '', color: 'gray', font: 'auto' } Subtext options
bgColor string '' Background color
bgFull string false Paints the background of the whole circle ignoring startAngle and endAngle
scale object { enabled: false, type: 'lines', color: 'gray', width: 4, quantity: 20, height: 10, spaceWidth: 15 } Scale options, type: lines or dots
step integer 1 Step change, min 0.1
displayPrevious boolean false Display previous value (true or false)
min integer 0 Min value (start value), only integer
max integer 100 Max value (end value), only integer
dynamicOptions boolean false Dynamic change options (true or false)

Contributing

  1. Fork the repo
  2. Install dependencies: npm install and bower install
  3. Run: grunt
  4. Make your changes
  5. Submit pull request

License

Licensed under the MIT license

ng-knob's People

Contributors

camilopalacios avatar gitter-badger avatar radmie avatar roibeart avatar ruisebastiao avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

ng-knob's Issues

Not able to display updated value

I am trying to fetch value from server but its sometimes not updating knob

$scope.value= 0;
$http.post(link).then(function (res){

$timeout(function() {
$scope.value = res.value;
},1000);

});

Feature Request: Zero-based reversing

Hi,

I would like to use the ng-knob as a zero-based knob.
For example, I have a knob which has a min/max of -100/100. I would like starting point to be zero.
Any positive values would be on the right of the mid-point and any negative values will be on the left.

Usage example: Grid power consumption.
If I am pushing power back into the grid (solar panels) it should swing on positive side and if I am using power it should be on the negative side.

Hoping someone brainier than I could help implement this?

Knob stops responding when value is greater than defined maximum

  1. let's have a knob with a defined maximum value (options.max)
  2. pass a value that's less or equal to the maximum value
  3. knob updates
  4. pass a value that's greater than the defined maximum
  5. knob updates to this value, but successive updates don't do anything (knob is frozen - doesn't react to changes to the value)

I would expect, that the value is either

  • displayed regardless of the maximum value
  • OR truncated to the maximum value
    and the arc shouldn't grow beyond the defined angles.

Here's a simple project that demonstrates this (it updates with a timer so just wait for it to freeze):
https://github.com/jakubhlavaty/ng-knob-bug

  • run sample with npm (see readme)
  • dependencies managed with bower (using ng-knob 0.1.3)

Dial Mode

This is a really solid component. I was looking for a dial / knob type of control for selecting a heading (from 0 - 360). The control allows it, but it'd represent 270 not as a needle or arrow pointing to the left, but as a radial chart. I essentially don't want to render the selected track, but do want to render something (a line or arrow) at the value's location on the track.

Knobs not updating options when in a ng-repeat

I am attempting to use the ng-knob directive in a ng-repeat, this works fine with initial options and values, but when updating the options for max within a ng-repeat the chart does not update/redraw.

Options work fine when the knob directive is outside of a ng-repeat.

Is there a way of getting ng-knob to reevaluate the options within a ng-repeat?

Insert additional text after the subText.

image

I was trying to insert text after the property "subText.text". In this case you can see that the value of this property is 'restantes' but if I want to add additional text that will not move and will stick to it, no matter the screen size I had to write a directive as follows. My question here...is there any standard way to do this?

I had the jsfiddle that shows that it works !! but is not a testable. I hope you guys let me know if my question is wrong and if it is please provide proper feedback.

export function KnobFormat($injector: any): angular.IDirective {

    return {
        restrict: 'A',
        link: function(scope: any, element: any, attrs: any){

            var svg = element.find('svg')[0];
            var $jq = angular.element; 
            var rows = $jq(element).find('text');
            var $filter = $injector.get('$filter')('filter');

            let pattern = {id: ''};
            var elements = $filter(rows, pattern, (a: any, e: any) => {  return (a !== 'text') ? true : false; });

            $jq(elements).css('font-size', '14px');
            $jq(elements).css('dominant-baseline', 'central');



            var text = svg.getElementsByClassName('sub-text')[0];
           

            var transform =  text.getAttribute('transform');
            var index1 = transform.indexOf('(') + 1;
            var index2 = transform.indexOf(')');
            var vals = transform.substring(index1, index2).split(',').map((n: string) => { return parseInt(n); });
            var x = vals[0];
            var y = vals[1] + 18;
            
            var newElement = document.createElementNS('http://www.w3.org/2000/svg', 'text');
            newElement.setAttribute('id', 'knob-text');
            newElement.setAttribute('class', 'sub-text');
            newElement.setAttribute('text-anchor', 'middle');
            newElement.setAttribute('font-size', 'bold italic large serifpx');

            var translate = 'translate(' + x + ',' + y + ')';
            newElement.setAttribute('transform', translate);
            newElement.style.fontSize = '12px';
            newElement.style.dominantBaseline = 'central';

            newElement.textContent = attrs.note;

            text.insertAdjacentElement('afterend', newElement);
        }
    };
}

Move the track bar instead of redrawing from zero on value update

Thanks for writing this great module! It's very easy to use.

i am working on a dashboard using ng-knob and it works well. i love the animation feature but since the update interval is quite frequent, i would prefer having the trackbar move between different value instead of redrawing it from zero on each update.

Thanks again. =D

Different color for barCap

Hey,
I really like this component, very well done!
I was wondering if there is a way to have the barCap (that I made round) with a border of a different color? I'd like to have a circle of a different color to make it easier for the users of my app to know they can click and drag the bar.
Thank you!

Responsive Knob - not a issue but question

Hello !

Thanks for this very good tool.
I use it in all my applications.

I plug my own directive knob. To be responsive i use a ratio during the link directive i am able to know the screen size. So i have a false responsive behavior. Just for the initialisation.

My question : is it possible to have a responsive behavior for the size knob object with css ?
i would like this kind of behavior :

.knob {
data-width: 50;
data-height: 50;
}
....max-width 480px {
data-width:35;
data-height:35;
}

Thanks for your attention, have a nice day.
alien from south of france where the english is not fluent.

Possible to use without Angular.js?

How much effort would it take to make this work with just d3.js on an otherwise static web-page? the use-case I'm thinking of just requires knobs for manipulating a data visualization, nothing more, and I don't know that it would be worth adding angular and dependencies just for that functionality.

How to "scale" the knob control?

Hello,

First of all, many thanks for this great piece of software. In fact this is not a "problem" or "error" report since the knob control works just fine, but, what about a possible control's "responsive" option? Too much complicated?

Anyway thanks very much for ng-knob.

Add option to update value from input

Can we add an option/property to make it possible to update the value from a input/textbox?

Similar to this http://anthonyterrien.com/demo/knob/

Would that be possible or do I need to add my own textbox and update the value based on textbox value?

It does has a displayInput property, but it just hide/show the value. But since it use svg I am not sure if it should have a input outside or maybe use foreignObject http://stackoverflow.com/a/5975783

http://caniuse.com/#search=foreignObject

he also refer to this http://www.carto.net/papers/svg/gui/textbox/

Angularjs updated value not updating ui-knob (chart)

Hello here is my html

<ui-knob id="mainKnob" value="value" options="options1"></ui-knob>

and js

.controller('AwardCtrl', function ($scope, getMembership){
  $scope.value = 10;

// called service
  getMembership.getMembership(userid).then(function(data) { 
       $scope.data = data; 
       $scope.value = parseInt(data.membership);
  });

  $scope.options1 = {
                animate : { enabled: true, duration: 1500, ease: 'bounce' },
                readOnly: true,
                size: 260,
                max: 31,
                subText: {
                    enabled: true,
                    text: 'DAYS LEFT',
                    color: '#f2f2f2;',
                    font: 'auto'
                },
                trackWidth: 55,
                barWidth: 30,
                trackColor: '#e6e6e6',
                barColor: '#81d8cf',
                dynamicOptions: true
            } 
})

But my knob is not updating after $scope.value has changed.

Nice to have : functions as color attributes

First, tanks for the work, it's really nice.
It would be very nice to be able to use function (with value as argument) for all color attributes.
This way, the color could vary in function of the value.

watch new value

Hi, amazing job on this control! Quick question - Would like to be able to watch value, and change options but not have it re-draw, merge all the options... would like to change colors on the fly.
thanks Marty

Make subtext visible even when value is not

For cases when displayInput: false, it would be good to have the subText.text visible. Of course, given that when subText.enabled: true. This is when the user doesn't want to see the value but just the text.

Change value options from HTML code

Hello, you can change subtext value from html ?

Example ...

<ui-knob value="value" options="options" subtext="'sample'" ></ui-knob>
or
<ui-knob value="value" options="{ baseoptions: options, subtext: 'sample' }" ></ui-knob>

thanks ...

circular slider fires a second time when touching anywhere on the screen

Hi,

We're working on a tablet app with a circular slider. When modify the value, it works fine, but the next time you touch the screen, the slider value is modified again. It seems to use the y coordinate of the location you touched to assign the new value. This only happens on second touch.

Any idea where this comes from ?

Add 0.1.4 Release To Git Hub

Bower, is not able to use the latest version of the code because the release marked is 0.1.3, However, it looks like you have made changes, as you bumped to .1.4. Can you update this ?

New features

I'm very happy using ng-knob 👍
but I would like you to make some improvements.

Features

  • Animate value like a count when value changed
  • Animate path circle when value changed

Fix

  • barCar causes some errors

I'm Brazilian, so my English is kinda bad. :)

Thanks

Step precision

Hello,

firstly, ng-knob is very nice component. Works great. My question is, are you considering to make "step" other type then integer?Unfortunately, I need to use step 0.5.

Thank you

readonly attribute

Hi,
I need to use readonly as a attribute not in the $scope.options.
what can i do?

Update ng-knob-dial value on mouse wheel events and keyboard events

I am using ng-knob in my project but I want to update knob-dial value on mouse wheel.
Like jQuery Knob when mouse wheel up then increase ng-knob-dial value and when down then decrease ng-knob-dial value

So, is it possible to update knob-dial value on mouse wheel up and down?

Also, is it possible to edit dial value by typing I mean by keyboard events?

negative start angles and mutability

I would like to use this control to emulate a traditional gauge starting at say 7 and ending at 5 O'clock on the face. There were no examples to do that, but setting startAngle to a negative value appeared to work. However, changing the knob's value seems to have issues when dragging past 0, or the left side of the widget.

For instance:
startAngle: -160
endAngle: 160

Am I usingItWrong™?

Can't create knobs whose arc crosses the 0° point

I would like to create a knob with a startAngle of about 210, which increases in value clockwise, and has an endAngle of about 150.

In other words, I'm basically trying to create the upside-down version of the "Angles" example on the demo page.

But, no matter what I do, I can't get ng-knob to draw a knob that crosses the 0° mark. Whenever I specify an endAngle that's less than the startAngle, I just get a knob with a narrow wedge that increases in value counterclockwise.

Could this be addressed by adding a spinDirection option, with possible values of "clockwise" and "counterclockwise"?

Or, alternately, with an angleBehavior option, with possible values of "useShortArc" and "useLongArc" (indicating whether to use the shorter of the two possible paths between startAngle and endAngle, or the longer)?

Great plugin, aside from this small detail!

How to call a function on "knob change"?

Hello,

I'm wondering how to use the knobs to call a js function? I implemented the knobs to my design and all looks nice but I'm not able to call a function on changing the knob.
What works is ng-mousemove="...()" but this calls my function every time I move the mouse over the knob.
I just want to call my function when the knob value got changed by moving the knob.

Is there a way to make this possible?
Are there any other ways to readout the knob value? For now I don't see the uasge of these knobs when I'm not able to work with the values...

Thanks in advance.
Bernhard

how I can handle events

In the comments say that: "touch, click and drag events implemented". How can i use those events
Thxs.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.