Is it legal..
That depends entirely on where you live and work. Google is your friend here.
... and/or ethical.
As long as they are only revealing "piecework" totals - and not base salary - then I'd say it's perfectly ethical. It's completely transparent - it takes something that is a definite and quantifiable measure of productivity and is using it to reward the most productive workers.
In addition, it will incentivize employees. Somebody leaving the shop floor at the end of a week can see that, if they'd matched Bob's work rate, they could have come out with an extra $100. That's giving them a definite and, more importantly, achievable goal. They know it's achievable because Bob just did it.
That's far preferable to somebody sitting behind a desk, plucking a number out of thin air, and saying "you should be able to do 800 of these a week" - that carries zero weight since he/she isn't making widgets all day.
Whether they post the number of units or the $ value makes no difference - everybody knows the unit rate and can calculate one from the other.
For what it's worth I run a few teams and am responsible for assigning annual bonuses from an assigned pot of cash. It's only logical the best performers should get the highest bonus - dividing the money equally is unfair to those who worked their butts off all year. Unfortunately I work in software development where it's difficult to quantify productivity or value to the company (there are ways, but they are either game-able or too subjective - lines of code, bugs raised/fixed etc.). This scheme is a perfect way to both improve productivity and ensure those who do the most work get better rewards.
The only potential problem I see is that, in the rush to churn out as many widgets as possible, quality may take a tumble.